JS Interview Preparation Cheetsheet

JS Interview Preparation Cheetsheet

·

4 min read

in This article, i am going to discuss about important topics of javascript. These topics are important for interviews . These topics are also confusing. but in this article i am going to try to make them easy for you. so lets just discuss These topics.

1. Scope :

Scope in JavaScript refers to the current context of code, which determines the accessibility of variables to JavaScript. Scopes can be globally defined or also can be locally defined. We can understand that anything carly braces are locally scoped. Lets understand different types of scopes.

( 1. ) Global scope :

An variable which is not inside any function or any curly braces ({}) is globally scoped. An global scope variable is accessable from anywhere in your code , even in function.

let name = 'iNeuron';
//this variable is not inside any
// function or curly braces so its globally scoped

function Global () {
   console.log(name)
// i can access inside any function or scope
}
console.log(name)
// i can access it from anywhere in the code

( 2. ) Local scope :

An variable which is not inside function or any curly braces ({}) is locally scoped. An local scope variable is only accessable inside the scope (inside the curly braces). we can not access it outside of scope.


function local() {
let name = 'Hitesh sir'
//this variable is inside the local function
//we can only access it from inside the function

   console.log(name)
// i can access inside any function or scope
}

// i can not access name here

( 3. ) Function scope :

The area of any function is the function scope for the declared variable inside the function. it means when we declare any variable inside the function they are only accessable inside the function we can not access outside the function.

function function() {
let name = 'Hitesh sir'
//this variable is inside the local function
//we can only access it from inside the function

   console.log(name)
// i can access inside any function or scope
}

// i can not access name here

( 4. ) Lexical scope :

when we define an function inside another function . The inner function will have access to the outer function's variable . This behavior is known as lexical scope.

function outer() {
let name = 'Hitesh sir'
//this variable is inside the local function

    function inner() {
         conosle.log(name)
       // name is accessable here
    }

   console.log(name)
// i can access inside any function or scope
}

// i can not access name here

2. Single Thread :

JavaScript is single threaded synchronous language. Now what it means that it executes code from top to bottom line by line. we can understand it by this simple example.

one()
// this line will be executed first
three() 
// this line will be executed second
two()
// this line will be executed third

function one() {
 console.log(' this is function one')
}

function two() {
 console.log(' this is function two')
}

function three() {
 console.log(' this is function three')
}

We can see in the example code that code is executed in order line by line It behaves like this because it has one call stack and one heap memory. we going to discuss about call stack later. for Now you can understand its part of memory where all the cade executes.

3. Call stack :

Call stack is a data structure which records the function calls, basically where in the program we are. Call Stack is the place where the code execution has been tracked.

If we call a function to execute , we push something on to the stack, and when we return from a function, we pop off the top of the stack.

When a javascript program gets run, javascript creates a global stack to keep track of functions and their execution.

You can think of the stack like a fancy array. The stack operates in a LIFO (last in first out) manner. When javascript tries to execute functions, the javascript engine will push the functions onto the stack, then iteratively pop them off the stack and execute them one by one. When the stack is empty, your program is finished!

That is all about call stack.

Thanks for reading . this is an short article about few important topics of javaScript. I will update this article in future for more important topics .