Variables are used to store data temporary in the memory. Variable can have any type of value i.e. number, string, boolean, date etc. The data stored in these variables can be changed in between the execution of the program. Variable name can be much longer but for convenient kept it short, easy and with descriptive name.
Rules to declare a variable name:
To declare a variable name, in JavaScript, camelCase is commonly used, each letter of every words except first word starts with a capital letter. eg: veryVeryLongVariable.
Example: Some Valid variable name: FirstName, firstname, first_name, Name1, _ , $
Example: Some Invalid variable name: 1Name, switch, if, First Name, user-name
JavaScript allow us to use a variable without declaring it. eg:
a=10; console.log(a); // 10
Above code is used without 'strict' hence no error, but in 'use strict' mode above code will give error.
"use strict"; a = 10; // error: a is not defined
Therefore, it is recommended that you must declare a variable before using it.
To declare a variable in JavaScript, use the let, var, const
keyword. For example:
let username; //variable declaration username='AnkitWebLogic'; //value assignment console.log(username); //display value
To declare multiple variables in one line, use the following syntax:
let student_name='Ankit', course='Web Development', percentage=99.99
let
is a modern way to declare a variable whereas var
is an old way.
<script> let a="Hi"; let a="Hi again"; // error - 'a' already declared document.write(a); // no output </script>
<script> var a="Hi"; var a="Hi again"; // no error document.write(a); // Hi again </script>
Constant variable are those variables whose value cannot be changed during the execution of the script. To declare a constant variable, const
keyword is used. Syntax: const
AADHAR_NUM=12345678910123456. It is recommended that use capital letter to declare constant variable, to distinguish between normal variable and a constant variable.
<script> const PI=3.1415; PI=5.5; //error, cannot be changed console.log(PI); </script>
Variable scope tells the user that where a variable can be used. JavaScript variable is of 2 types: Local and Global.
The variable declared inside a function has a local scope, it cannot be access outside that function.
<body> <h2>JavaScript Scope</h2> <p id="demo1"></p> <p id="demo2"></p> <script> exLocalVariable(); //function call function exLocalVariable() { let interest = "Computer Games"; //local variable declaration demo1.innerHTML='Type:'+ typeof interest +" Value:"+ interest; } demo2.innerHTML = 'Type:' + typeof interest; </script> </body>
The variable declared outside a function has a global scope, it can be access in other functions.
<body> <h2>JavaScript Scope</h2> <p id="demo1"></p> <p id="demo2"></p> <script> let interest = "Computer Games"; //global variable declaration exLocalVariable(); //function call function exLocalVariable() { demo1.innerHTML = 'Inside Function: Type:' + typeof interest + " Value:" + interest; } demo2.innerHTML = 'Outside Function: Type:' + typeof interest + " Value:" + interest; </script> </body>
Ad: