Variable:
- Variable is an identity of memory location.Variable is used to store the information.
- In JavaScript, we represent the variables either by using var keyword or let keyword
Syntax: | Example: |
var identity; or let identity; | var name = “amar”; var age = 23; var salary = 35000.00; |
Rules to create variable:
- Variable name contains Alphabets (a-z or A-Z) or Digits(0-9) or Symbols ($ or _)
- Variable name should not start with digit
- var 9a = 10 ; (error)
- var a9 = 10 ; (no error)
- Only two symbols allowed in variable name ($, _ )
- var acc-num = 1234; (error)
- var acc_num = 1234; (no error)
- Variables are case sensitive. Hence we can define multiple variables with same name with different case.
- var a=10, A=20; (no error)
- Variable name should not match with keywords
- var for=10 ;
Variable Declaration, Assignment, Modify and Initialization:
Declaration: A variable without value is called declaration
var a; let b; |
Assignment: Assigning value to variable which is already declared.
var a; // declaration a = 10; // assignment |
Modify: Updating the existing value of variable.
var balance; // declaration balance = 3000 ; // assignment balance = balance – 1400 ; // modify or update |
Initialization: Assigning a value at the time of declaration
var x = 10 ; // initialization |