Updated date:

Javascript - Variables and Scope

Author:

I am Lakshmi. I've completed my graduation in the stream of ECE. I love reading and like to listen more than I speak up.

Javascript Variables

A variable is a placeholder for data, and the data should belong to any valid datatypes of Javascript. Each variable should have a unique variable name called the identifier where those identifiers are case sensitive(the variable ‘A’ and ‘a’ are different).

var vehicle ; //variable declaration

vehicle = “car”; //variable initialization

var vehicle = “car”; //variable initializing at the time of declaration

var vehicle, mode, number; // declaring multiple variables
javascript-variables-and-scope

Variable Scope

In javascript, the variable is assigned with three different types of keywords, such as var, let, and const, to define its scope. The scope defines up to which extent a variable is valid in a program.

Global Variables and Local variables

  • The variable declared without keyword is categorized under a global variable irrespective of its place of declaration.
  • The variable declared outside of all the functions has global scope(it is accessible throughout the program without passing as a parameter into the function), and it can use the var keyword.
  • Local variables have a lifespan within a function and block.
javascript-variables-and-scope

Var, Let, and Const

Var

  • As we have seen earlier, A variable declared with a var keyword has a global scope when it is declared outside the function.
  • It is limited to the function scope when declared inside the function. but, if it is declared within a block, we can access it outside the block.
  • It can be re-declared and updated within its scope.


javascript-variables-and-scope

let

  • The variable declared with a let keyword has a global scope when it is declared outside the function.
  • A variable declared with a let keyword can be accessible within a block, where block defines the group of conditional statements within the curly braces(if,if-else, for, while,etc.,
  • It cannot be re-declared, if we try to re-declare the variable, we will get a syntax error.


javascript-variables-and-scope
  • We can update the variable, but the issue is the scope of the variable will change to global.
javascript-variables-and-scope

const

  • A variable declared with const keyword is constant forever, if it is declared outside the function it will have global scope. When it is declared inside a function, it will have local scope.
  • It cannot be re-declared within its scope, if we try to re-declare the variable, we will get a syntax error.


javascript-variables-and-scope
  • We cannot update or re-assign the value of const, doing so will produce TypeError.
  • We should Initialize the const variable at the time of declaration, If we try to assign the value later, Javascript will produce TypeError.
javascript-variables-and-scope

This content is accurate and true to the best of the author’s knowledge and is not meant to substitute for formal and individualized advice from a qualified professional.

© 2020 Lakshmi