Print Variable Javascript
In this article, we will see about print variable in javascript.
We know that in the javascript name followed by pair of parenthesis is identified as a method or function. Hence writing is a method or function, because it is followed by pair of parenthesis or brackets.
The wirte method is present within the JavaScript document object; hence we say the write method is a member method of the document object.
To access the write method we use the dot operator (i.e. also known as the member access operator)
JavaScript writes method is used to write or display some content in the browser window.
The content passed to the write method is also known as a parameter or argument.
To the write method, we can pass a single value or an expression i.e. single argument.
Ex:
document.write(2);
document.write("Hello World");
To the write method, we can pass multiple values separated by commas i.e multiple arguments.
Ex:
document.write(2+2);
document.write("2"+"2");
document.write(2, 2);
document.write("2", "2");
Note:
- () parentheses are evaluated inner most to outer most+
- operators are evaluated form left to right.
- , comma separators are evaluated from left to right.
Example code for printing variables in javascript
let name= "HELLO WORLD ";
document.write(name);
/*
Output:
HELLO WORLD
*/
In the above, the let is a datatype and “name” is a variable. Then we assign the value “HELLO WORLD” to a variable name. And using document.write a method to print the variable “name”. So, the output comes “HELLO WORLD”.