WebDevelopersNotes logo

home-icon Home / JavaScript / Online JavaScript Manual – Variables and JavaScript methods

Online JavaScript Manual – Variables and JavaScript methods

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.

Sponsored Links

Your comments
Star icon IMPORTANT Have a question / problem? Click here to ask an expert.

Web browser logos recreated

Sponsored Links

Tips

How do I view a deleted web page?
Is there a way to view a deleted web page - one that is no longer available? Yes there is and the solution is quite simple. [more...]

Fact

b2/cafelog written in PHP by Michel Valdrighi is the predecessor of WordPress. The popular blogging platform was developed by Matt Mullenweg and Mike Little in 2003. [more...]

We use cookies to give you the best possible website experience. By using WebDevelopersNotes.com, you agree to our Privacy Policy