//For Example var no=4; //which stores value 4 in no variable var name="Surya"//String var bool=true//Boolean which is either true or false
Declare a variable:
For example :Use the var keyword to create a variable called yourName
Storing Values with the Assignment Operator(=):
var myVariable; myVariable=5//This assigns the Number value 5 to myVariable.
For example :Lets use the var keyword to create a variable called age and assign value of 18 to age .
var age; age=18;
Assigning the Value of One Variable to Another Variable:
After a value is assigned to a variable using the assignment operator, you can assign the value of that variable to another variable using the assignment operator.
var myVar; myVar = 5; var myNo; myNo = myVar;
//In this Program lets swap values between **a** and ** b** variables. var a=5; var b=10; var c; c=b;//here c=10 b=a;//In this line value **5** is assigned to variable **b** a=c;//In this line value **10** is assigned to variable **a** //we can console the value to check the swapping console.log("a value is "+a); console.log("b value is "+b);
Initializing Variables with the Assignment Operator(=):
It is common to initialize a variable to an initial value in the same line as it is declared.
var myVar = 0; //Creates a new variable called myVar and assigns it an initial value of 0.
Declare String Variable:
We can declare String variable similar to numbers ,but you have to store the value in " ". For example:
var myFirstName="Surya";//Surya is assigned to myFirstName variable var myLastName="L";//L is assigned to myLastName variable
For example: Initialize the three variables a, b, and c with 5, 10, and "I am a" respectively so that they will not be undefined.
var a=5; var b=10; var c= "I am a"; a = a + 1;//From above 'a' we get 5 and this line we add 1 to variable a b = b + 5;//From above 'b' we get 10 and this line we add 5 to variable b c = c + " String!";//From above 'c' we get "I am a" value of string and this line we add "String!" to variable C
Case Sensitivity in Variables
We can create a read only variable which can declared using const keyword. For example :
const pi=3.14; //The value of **pi** cannot be changed and this type of variable is called as Immutable Variable console.log(pi);
Credits: I learned this topics in FreeCodeCamp which I explained in minified version
Thanks for reading the blog. Do let me know what you think about it.