/  Technology   /  VAR Variable
VAR Variable

VAR Variable

 

What is var?

A variable in JavaScript stores the data value that can be changed later. To declare a variable in JavaScript, use the reserved keyword var.

Declaring a variable with var keyword but the value has not been defined to this variable is known as uninitialized variable.

var Var;

For checking value for Declared variable we can use console.log(Variable);

console.log(Var);
console.log(typeof(Var));

We can check the typeof() data stored in the variable by logging

console.log(typeof(Var));

The logged value of the uninitialized variable is undefined.

Output:

v1

  1. Assigning number to the variable and checking the type of variable
var Var;
Var=5;
console.log(Var);// 5
console.log(typeof(Var));  // number
  1. Assigning string to the variable and checking the type of variable.

We can denote string in 3 ways

  • Single quotes
  • Double quotes
  • Backtick
var Var='String';
console.log(typeof(Var)); // string
var Var="String";
console.log(typeof(Var)); // string
  1. Assigning Boolean value to the variable and checking the type of variable.
var Var=true;
console.log(typeof(Var)); // boolean
  1. Assigning null value to the variable and checking the type of variable.
Var=null;
console.log(typeof(Var)); // object
  1. Assigning array to the variable and checking the type of variable.
var array= ['null',"array",`object`]
console.log(typeof(array)); // object
  1. How to check length of an array?
console.log(array.length);  //3
  1. Assigning object to the variable and checking the type of variable.
var object= {var1:'null',var2:"array",var3:`object`}
console.log(typeof(object)); // object

Note: For these 3 variable types null, array and object  The logged value of variable  is object

Final output:

v2

Redeclaring a Variable Using var keyword

<html>
<title>
Redeclaring a Variable Using var keyword
</title>
<body>
<script>
let name = "Sailaja"; // Here name is Sailaja
{
let name = "vani";  // Here name is vani
}
let name = "vani1";
document.write("name is", " : ",name)

</script>
</body>
</html>

 

Leave a comment