JS Variables

What is Variable ?

  1. JavaScript Variable is called as a container which can store value or expression.
  2. In JavaScript, Value / Expression can be assigned to variable.
  3. Variable can hold only one value at a time.

How to declare Variable in JavaScript ?

var variable_name;

Explanation :

  1. We can declare variable using “var” keyword.
  2. We can declare one or more variable at a time.
  3. We can initialize variable at the time of declaration.
  4. We can declare “n number” of variables as per requirement.

Different methods of declaring JavaScript variables

  1. Declaring One JavaScript variable
  2. Declaring Multiple JavaScript variable.
  3. Declaring and Assigning One JavaScript Variable.
  4. Declaring and Assigning Multiple JavaScript Variables.

Example :

// Declaring one JavaScript Variable
var sub1;

// Declaring Multiple JavaScript Variables
var sub1, sub2;

// Declaring and Assigning one JavaScript Variable
var sub1 = 78;

// Declaring and Assigning n JavaScript Variable
var sub1 = 78 , sub2 = 90;

Rules for Declaring JavaScript Variables :

  1. Variable name contain any Letter of the alphabet, Digits (0-9), and the Underscore character.
  2. Spaces,Punctuation marks and Special Symbols are not allowed.
  3. The first character of a variable name cannot be a Digit.
  4. JavaScript variable’s names are Case-Sensitive.
  5. For example sub1 and Sub1 are two different variables.

Valid Variable Name :

Variable Name Status Reason
Pri Valid Capital at start allowed
SUM Valid Variable name in CAPS is allowed
suM Valid Combination of small and big letters is allowed
num of element Invalid Space not allowed
10num Invalid Digit at start is not allowed