Online JavaScript Manual – Variables and JavaScript methods

Online JavaScript Manual – Variables and JavaScript methods cover image
  1. Home
  2. JavaScript
  3. Online JavaScript Manual – Variables and JavaScript methods

Till now we’ve looked at two important methods, the alert() method of the window object and write() of document object. You’ve also seen how text or numbers can be passed to these methods.

Let’s consider a situation where you plan to display the name of the visitor on the page. To do this you would first have to get the visitor’s name (we’ll shortly see how to achieve this), store it in a variable and send the result to the document.write() method. Here we’ll examine how we can pass variables to JavaScript methods.

document.write("Your name is " + vis_name);

We’ve assumed that the visitor’s name is stored in a variable called vis_name. Note how the variable is written outside the quotes of the write() method. If this variable was included in the quotes, JavaScript would have written vis_name instead of the name of the visitor. Similarly, for the alert() method, we would pass the value as:

alert("Welcome " + vis_name + "\nHow are we today?");

Remember, a variable should be placed outside the quotes to display its value.

JavaScript