Our Hero Javascript
Javascript is a single threaded synchronous Scripting and Programming language Don't worry In Next section I explain about single threaded and synchronous
Javascript mostly for web dev. javascript allows you to implement complex features on web pages and manipulate the DOM
JavaScript was invented by Brendan Eich in 1995. It was developed for Netscape. when javascript lounch it's name was Mocha, After some time it's name become LiveScript and now it finally named Javascript.
Execution Context - Hoisting - Single Thread
Execution happened in javascript in Two Steps:
Scaning ( First Step )
First javascript scan the code and allocate memory to variables, functions etc
When JS allocate memory to variables it's store like key : value pare. The key is variable name
and value is set as undefined
, and in case of functions the key is function name
and value is The Whole code base under that function
Code Execution ( secend Step )
There now javascript actually start executing code and assign value to variables, In this case javascript assign "Rohan" to the variable name
When we run any javascript code then in CallStack a Global execution context created. In the billow example we can see how javascript execute code in the callstack
Hoisting
When you accessing a variable before creating the variable, that is call Hoisnting .
Intresting thing is everytime you got Undefined In case of varables because in scanning part javascript assign undefined to every variable and in codeExecution part before javascript assgin the value, you console log the variable.
But if you try to call a function before creating it, boom you got output
So why In case of functions you got the output and in case of variables you got undefined
Now as you know in javascript store variable data in key value pare. in scanning part key is variable name
and value is undefiend
, and for function key is function name
and value is the whole code under that function
.
So when you call a function the whole code under that function
just execute and you got the output
Note : callback function treated as variables
Temporal dead zone
In simple word we can say the time period a variable value is undefiend
, that time period for that variable is called Temporal dead zone.
Concept of Scope
To build a stronger foundation of Javascript, you should understand its key features. Out of these, Scope is the first and most significant concepts for coders to learn.
Scope
A scope in Javascript determines the accessibility of variables, objects and function in your code during runtime. This mean by Scope you can find where you can use your variables, objects, functions and where you can't
Local Scope and Global Scope
There is two type of Scope in javascript Local Scope and Global Scope
Global Scope
There is only one Global scope throughout a JavaScript Program. When you begin writing code in JavaScript, you are already in the global scope.
If you declare any variable outside of a function, then it belongs to the global scope and is therefore accessible from anywhere in your code.
let num = 10; // This is a Global variable and it is accessible anywhere in the codebase
function add() {
//Local Scope
let num = 50; //This num is accessible only under this curly braces
let num2 = 20;
}
{
// This is also a local scope. If I create a variable hare then I can't access that variable outside of this curly braces
}
console.log(num); // 10
If you try to console.log num2 then it gives a ReferenceError Error.
In above example variable num scope is global means num is acceable in anyware in the codebase.
and under the add() function whatever variable we declear, there scope is local means the num2 and num under the function is only acceable in the add() function
Importent note about var
If you declear variable using var
then now matter where you decler the variable it's scope is Global
Var did not follow Scop concept
Single Thread
Why Javascript is a single threaded language ??
Beacuse javascript has only one CallStack. If javascript has two callstack then javascript is called dubble threaded languages