Preface First, a big thanks to all of you for supporting my last series,
Introduction
I enjoyed the interaction with all of you -- reading your comments, questions, and messages. I feel excited and I am starting a new series called “Inquisitive Developer – JavaScript”. This new series will go in-depth about JavaScript programming language. I will try my best to take you on this programming journey and cover common mistakes, misconceptions, and cool features of the King of scripts, JavaScript.
I welcome you all to the new series!
Getting started
Let me start with a basic keyword ‘var’.
> var variableName = ‘assignment value’;
There are two parts here, i.e. declaration and initialization. I will split them as:
> var variableName; // Code 1
Code 1 will declare variable only and the default value assigned is ‘undefined’. JavaScript assigns undefined value, as the purpose is to assign default value to this variable. The benefit of undefined is that a developer may not know the value of the variable during declaration.
Code 2 JavaScript interpreter interprets it as declared and initialized together,
> var variableName;
> variableName = ‘assignment value’;
To understand ‘hoisting’ we will first learn scoping.
JavaScript follows lexical scoping (static scoping). Lexical means vocabulary of a language. Scoping means the scope of a variable. There are two types of variable scopes global and local.
A variable that is declared outside the function. This variable can be accessible throughout the program, for example,
> var variableName = ‘assignment value’; // Code 2
> variableName = ‘assignment value’;
Scoping
Global scope
- var x = 90; //
- function f1() {
- console.log(x);
- };
- f1(); // output will be 90
- var x = 90;
- function f1() {
- console.log(x);
- };
- f1();
- console.log(window.x); //output 90
Question: Will it work and give the same output?
- x = 90;
- function f1() {
- console.log(x); //output 90
- };
- f1();
Local scope (Function-level scope)
- function f1() {
- var x = 1; // x is a local variable
- console.log(x); // prints 1
- } f1();
- function f1() {
- var x = 1;
- console.log(x); // print 1
- if (true) {
- var x = 2;
- console.log(x); // print 2
- }
- console.log(x); // print 2
- };
- f1();
No block-level scope
- function f1() {
- var x = 1;
- console.log(x); // print 1
- function f2() {
- if (true) {
- var x = 2;
- console.log(x); // print 2
- }
- };
- f2();
- console.log(x); // print 1
- };
- f1();
A misconception
- for (var index = 0; index < 5; index++)
- {
- console.log(index); // will simply print 0 .. 4
- }
- console.log(index); // print undefined
- for (var index = 0; index < 5; index++)
- {
- console.log(index); // print 0 .. 4
- }
- console.log(index); // print 5
Priority
- var index = 9;
- (() =>
- {
- for (var index = 0; index < 5; index++)
- {
- console.log(index); // print 0..4
- }
- })();
- console.log(index); // print 9
Variable Hoisting
- console.log(hoist1); // prints undefined
- var hoist1 = 'hoisting';
- console.log(hoist1); // prints hoisting
- var hoist1;
- console.log(hoist1); // prints undefined
- hoist1 = 'hoisting';
- console.log(hoist1); // prints hoisting
Hoisting precedence
- var f1; // variable declaration is done
- function f1(params) {
- console.log('its function1');
- };
- console.log(typeof f1); //print function
- var f2 = 'its variable'; // variable assignment
- function f2(params) {
- console.log('its function2');
- };
- console.log(typeof f2); // print string
Rules of variable definition
- Never create undeclared variables, because these are created as global.
- Undeclared variables unless assigned value give an error if they are referenced, example-
> console.log (a);
The reason is all declared variables are hoisted on top,

- Undeclared variables can be deleted:Sample one
- a = 10;
- console.log(a); // print 10
- delete a;
- console.log(typeof a); // undefined
- console.log(a); // below error
Sample two- var a = 10;
- console.log(a); // print 10
- delete a;
- console.log(typeof a); // number
- console.log(a); // print 10
Hoisting functions and expression
The function expression is not hoisted, please refer to code below,
- fncHoisted(); // TypeError: undefined is not a function
- fncNotHoisted(); // Definition hoisted!
- function fncHoisted() {
- console.log("Definition hoisted!");
- }
- var fncNotHoisted = function()
- {
- console.log("Definition not hoisted!");
- };
Block-level scope
Scoping rules
- Unlike var keyword, let provides block and sub-blocks scope. It is similar to other programming languages.
- function f1()
- {
- let name = 'Sumit';
- if (true) {
- let name = 'Jolly';
- }
- console.log(name); // print Sumit
- }
- f1();
- Unlike var, let the keyword does not create a property in a global object, i.e., window.
Output
- var x = 'associate with window object.';
- let y = 'no association.';
- console.log(window.x);
- console.log(window.y);

- Duplicate variables are not allowed,
- var y = 10;
- var y = 20; // this is allowed using var
- let x = 10;
- let x = 20; // duplicate x is not allowed

- Always good to declare the variable using let before referencing it, here you can see the difference:Using letIt will give reference error,Using var
- function do() {
- console.log(foo); // ReferenceError
- let foo = 2;
- }
- do();
It will print undefined rather than error,- function do()
- {
- console.log(foo); // undefined
- var foo = 2;
- }
- do();
Summary
I hope you enjoyed reading this article. Please share your comments/feedback.
Vishal MisquittaPosted Sep 13, 2016, 7:48 AM
Nice Article
Samir KhimaniPosted Aug 3, 2016, 2:45 AM
Nice Article for understanding basics of javascript
Vignesh ManiPosted Jul 18, 2016, 6:28 PM
Good one
Muhammad Aqib ShehzadPosted Jul 18, 2016, 8:11 AM
Very nice elaboration
kalu singh raoPosted Jul 18, 2016, 1:55 AM
Nice...
Anupam SinghPosted Jul 18, 2016, 1:49 AM
"the King of scripts, JavaScript", now advance series. thanks.
Guest UserPosted Jul 17, 2016, 9:45 PM
Thanks Ano Mepani for your kind words!
Ano MepaniPosted Jul 17, 2016, 3:30 PM
Sumit Sir,I enjoyed your article, its very useful to understand basic JavaScript and you have represented very well. Thanks for such wonderful article.
Guest UserPosted Jul 17, 2016, 2:05 AM
Thanks Rakesh..!
Guest UserPosted Jul 17, 2016, 2:05 AM
Thanks Anil, I am glad you liked it!
RakeshPosted Jul 17, 2016, 1:52 AM
Good share sirji
Anil KumarPosted Jul 17, 2016, 1:52 AM
Nice read! I like the way you present :)
Guest UserPosted Jul 16, 2016, 9:31 PM
Thanks a lot Mahesh!
Mahesh ChandPosted Jul 16, 2016, 7:17 PM
Nice. After success of one series, second started.