DEV Community

Ranjith srt
Ranjith srt

Posted on • Edited on

Js | Variable & Values |

Jscript print :


console.log("Hello World ");

Enter fullscreen mode Exit fullscreen mode

Variables & Values

Image description

Image description

  • 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.

Enter fullscreen mode Exit fullscreen mode

What is Not Good?


- var does not have to be declared.

- var is hoisted.

- var binds to this.

Enter fullscreen mode Exit fullscreen mode

Declare var variable :


var a; #Error undefined 

Enter fullscreen mode Exit fullscreen mode

Initialize the variable :


var a = 10;

Enter fullscreen mode Exit fullscreen mode

Redeclaration allowed :


var a = 100;
console.log(a);

Output :
100

Enter fullscreen mode Exit fullscreen mode

Declare let variable :


let b;

Enter fullscreen mode Exit fullscreen mode

Initialize the variable :


let b = 10;

Enter fullscreen mode Exit fullscreen mode

Redeclaration is not allowed :


let b =100; 

Enter fullscreen mode Exit fullscreen mode

Reassignment is allowed :


b = 200;
console.log(b);

Enter fullscreen mode Exit fullscreen mode

cons must Declare & initialize 1st line :


const PI=31415 ;
//fixed Value Called Constant
console.log (PI);

Enter fullscreen mode Exit fullscreen mode

Reassignment is not allowed :


const c = 1;
c = 2;
console.log(c);

Enter fullscreen mode Exit fullscreen mode

Naming conventions :

correct way :


Correct camel Case convention :
let userName = 'John'; 

Enter fullscreen mode Exit fullscreen mode

Underscore is not recommended but valid :

let user_Name ='Max'; 

Enter fullscreen mode Exit fullscreen mode

start from Underscore :

let _userName ='Doe'; 

Enter fullscreen mode Exit fullscreen mode

start from dollar symbol :

let $userName='Evo' ; 

Enter fullscreen mode Exit fullscreen mode

string concatenate :

console.log('Welcome + userName);
console.log("Hi + userName);
console.log('Bye + userName);

Enter fullscreen mode Exit fullscreen mode

Incorrect variable names :

Invalid: variable names cannot start with a number


let 5date= 5;

Enter fullscreen mode Exit fullscreen mode

Resrved keywords (Functions):


let new = "data";
let function = 20;

Enter fullscreen mode Exit fullscreen mode

Symbols in variable names :


let #function = 'data'; 
let @function = 'data';
let John&Jane = 'Friends'; 

let $function = 'data';  
#this one is correct

Enter fullscreen mode Exit fullscreen mode

Scope of variables will on functions & loops :

var: Function scoped.
let: Block scoped.
const: Block scoped 

Enter fullscreen mode Exit fullscreen mode

Top comments (0)