var mgs = "Hello, World!"; //stored text message in "mgs" variable console.log(mgs) //called the varibale by its name to get the stored value //expected result: Hello, World!
var keyword, variable name, the assignment operator and the variable value.
var keyword, later you can assign the value using
varName = varValue syntax.
Calling a variable that does not contain a value will output
Initially to declare a variable you must have to use the
var var1; //declare a single variable without assigning a value var1 = 10; //Assigned value as 10 to the var1 variable. ... var var1 = 10; //declare a single variable with assigning a value // both are same ... var var1, var2, var3; //declare multiple variable without assigning values var1 = 10; var2 = 12; var3 = var2 - var1; //assigned values separately ... //it also can be var var1 = 10, var2 = 12, var3 = var2 - var1;
Type of values in JS variable
" " ) or a single quotation (
' ' ).
var myString = "Hello, World!"; //String data with double quotation var myString = 'Hello, World!'; //String Data with double quotation
var myNumber = integervalue; syntax. Note that, while assigning an integer value to a variable, you don’t have to use single or double quotation like string.
var myNumber = 10; ... //don't mess up by putting quotation while declaring integer or number data
Booleans data refer to the logic true and false. This kind of data or logic repeatedly used in conditional statement. Boolean can be declared in variable like below:
var myLogic = true; //Don't need to put a quotation var myLogic = false;
var myArray = [1, 'Mike', 'Alex', 10]; ... //This array contain two numbers and two string values Mike and Alex. //Notice that, string or text value uses quotation but number does not
document.querySelector('p'); //It is an object, let's assign it to a variable ... var myObject = document.querySelector('p'); .. //In this way, we can assign any statement in a variable