top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

JavaScript Variable

0 votes
217 views

Variable means anything that can vary. JavaScript includes variables which hold the data value and it can be changed anytime.

JavaScript uses reserved keyword var to declare a variable. A variable must have a unique name. You can assign a value to a variable using equal to (=) operator when you declare it or before using it.

Syntax:

var <variable-name>;

var <variable-name> = <value>;
            

Example: Variable declaration & initialization

var one = 1; // variable stores numeric value

var two = 'two';  // variable stores string value

var three;  // declared a variable without assigning a value

In the above example, we have declared three variables using var keyword: one, two and three. We have assigned values to variables one and two at the same time when we declared it, whereas variable three is declared but does not hold any value yet, so it's value will be 'undefined'.

Declare variables in single line:

Multiple variables can also be declared in a single line separated by comma.

Example: Multiple variables in single line

var one = 1, two = 'two', three;

Declare variable without var keyword:

JavaScript allows variable declaration without var keyword. You must assign a value when you declare a variable without var keyword.

Example: Variable without var keyword

one = 1;

two = 'two';

It is Not Recommended to declare a variable without var keyword. It can accidently overwrite an existing global variable.

Scope of the variables declared without var keyword become global irrespective of where it is declared. Global variables can be accessed from anywhere in the web page. Visit Scope for more information.

White space and line breaks:

JavaScript allows multiple white spaces and line breaks when you declare a variable with varkeyword.

Example: Whitespace and line breaks

var
             one
         =

         1,
            two
            =
            "two"

Please note that semicolon is optional.

Loosely-typed variables:

C# or Java has strongly typed variables. It means variable must be declared with a particular data type, which tells what type of data the variable will hold.

JavaScript variables are loosely-typed which means it does not require a data type to be declared. You can assign any type of literal values to a variable e.g. string, integer, float, boolean etc..

Example: Loosely Typed variables

var one =1;  // numeric value

one = 'one'; // string value

one= 1.1; // decimal value

one = true; // Boolean value

one = null; // null value

Points to Remember :

  1. Variable stores a single data value that can be changed later.
  2. Variables can be defined using var keyword. Variables defined without varkeyword become global variables.
  3. Variables must be initialized before using.
  4. Multiple variables can be defined in a single line. e.g. var one = 1, two = 2, three = "three";
  5. Variables in JavaScript are loosely-typed variables. It can store value of any data type through out it's life time.
posted Feb 7, 2017 by Shivaranjini

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

ES(ECMASCRIPT) 6 provides two new ways of declaring variables: let and const, which mostly replace the ES5 way of declaring variables, var.

LET

let works similarly to var, but the variable it declares is block-scoped, it only exists within the current block. var is function-scoped.


function order(x, y) {
    if (x > y) { // (A)
        let tmp = x;
        x = y;
        y = tmp;
    }
    console.log(tmp===x); // ReferenceError: tmp is not defined
    return [x, y];
}

CONST
const works like let, but the variable you declare must be immediately initialized, with a value that can’t be changed afterwards.

const allows you to declare a single assignment variable lexically bound. 


const foo;
    // SyntaxError: missing = in const declaration

const bar = 123;
bar = 456;
    // TypeError: `bar` is read-only

VAR
With const and let, there is no more space for var anymore.

 

Video Tutorial

https://www.youtube.com/watch?v=zfCATpShE5g

READ MORE
...