Variable Scope in Javascript

Vivek Sharma
The Startup
Published in
3 min readSep 13, 2020

--

Introduction:

This article is first part of very important topics in javascript which covers the basics of programming in javascript.

In this article, we will discuss scope in javascript. We will cover different types of variable scopes available in javascript with some examples and also briefly cover new variable declaration keywords introduced in ES2016.

But first, we will cover difference between variable declaration and assignment. Let’s look into the difference between them.

Variable Declaration vs Assignment:

A variable declaration simply tells the interpreter that a variable exists. By default, it is initialised as undefined.

var studentName; console.log(studentName);

Thing to note here:

  • We have declared a variable studentName. By default it is initialised as undefined.
  • It does not throw ReferenceError - studentName is not defined because it is initialised by default.

Variable assignment refers to assignment of value to the variable.

studentName = 'John Doe';

We can both declare and assign value to a variable in single line:

--

--

Vivek Sharma
The Startup
0 Followers

Frontend Developer | Web Developer | Mobile App Developer | Freelancer | Blogger | Techie