Jscript print :
console.log("Hello World ");
Variables & Values
The var keyword was used in all JavaScript code from 1995 to 2015.
The let and const keywords were added to JavaScript in 2015.
The var keyword should only be used in code written for older browsers.
Java script identifiers :
All JavaScript variables must be identified with unique names.
These unique names are called identifiers.
Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).
The general rules for constructing names for variables (unique identifiers) are:
Names can contain letters, digits, underscores, and dollar signs.
1.Names must begin with a letter.
2.Names can also begin with $ and (but we will not use it in this tutorial).
3.Names are case sensitive (y and Y are different variables).
4.Reserved words (like JavaScript keywords) cannot be used as names.
What is Good?
- let and const have block scope.
- let and const can not be redeclared.
- let and const must be declared before use.
- let and const does not bind to this.
- let and const are not hoisted.
What is Not Good?
- var does not have to be declared.
- var is hoisted.
- var binds to this.
Declare var variable :
var a; #Error undefined
Initialize the variable :
var a = 10;
Redeclaration allowed :
var a = 100;
console.log(a);
Output :
100
Declare let variable :
let b;
Initialize the variable :
let b = 10;
Redeclaration is not allowed :
let b =100;
Reassignment is allowed :
b = 200;
console.log(b);
cons must Declare & initialize 1st line :
const PI=31415 ;
//fixed Value Called Constant
console.log (PI);
Reassignment is not allowed :
const c = 1;
c = 2;
console.log(c);
Naming conventions :
correct way :
Correct camel Case convention :
let userName = 'John';
Underscore is not recommended but valid :
let user_Name ='Max';
start from Underscore :
let _userName ='Doe';
start from dollar symbol :
let $userName='Evo' ;
string concatenate :
console.log('Welcome + userName);
console.log("Hi + userName);
console.log('Bye + userName);
Incorrect variable names :
Invalid: variable names cannot start with a number
let 5date= 5;
Resrved keywords (Functions):
let new = "data";
let function = 20;
Symbols in variable names :
let #function = 'data';
let @function = 'data';
let John&Jane = 'Friends';
let $function = 'data';
#this one is correct
Scope of variables will on functions & loops :
var: Function scoped.
let: Block scoped.
const: Block scoped
Top comments (0)