Javascript interview cheatsheet
What is JavaScript?
JavaScript is a lightweight, interpreted, object-oriented language, and is best known as the scripting language for Web pages
JavaScript runs on the client side of the web, which can be used to design / program how the web pages behave on the occurrence of an event. JavaScript is an easy to learn and also powerful scripting language, widely used for controlling web page behavior.
Scope
Scope in Javascript is known as current context of code, Which determine the accessibility of variables to javascript
. The three type of scopes are Global Scope
, Block Scope
, Function Scope
.
Before ES6 (2015), JavaScript had only Global Scope and Function Scope. ES6 introduced two important new JavaScript keywords: let and const.
These two keywords provide Block Scope in JavaScript.
Variables declared inside a { }
block cannot be accessed from outside the block.
Global Scope
Variables declared outside function scope or block scope have global scope
let car = "Tata"; //This variable can be accessed anywhere in Javascript
function testCar(){
// variable can be used here
}
// variable can be used here
{
// variable can be used here
}
Function Scope
Variable declared inside any function can be used inside that function only
function testCar(){
let car = "Tata"; //This variable can be accessed inside this function only
// variable can be used here
{
// variable can be used here
}
}
// variable cannot be used here
{
// variable cannot be used here
}
Block Scope
Variable declared inside {}
have block scope they cannot be used outside of it own block scope
{
let userName = "Rohtash"; // this variable can be used inside this block level scope
function welcome(){
console.log("Hii "+userName+", welcome to my blog");
{
console.log(userName); // this can be used here to because userName still is in it block
}
}
}
Javascript is single threaded language
if you using javascipt for a while then you may come over with the statement that Javascript is single threaded language
.
what does that means ?
Javascript is single threaded means one statement will be executed at one time.
console.log('1'); // this will go first
setTimeout(()=> {
console.log('2') // this will be executed after executing of log("3")
}, 3000);
console.log('3'); // this will go second
In javascript everything goes in callstack and as the statement executed one by one callstack pop out the task that have be executed.
What is CallStack in Javascript
Callstack is mechanism for javascript interpreter to keep the list of the statement of code and pop out them if the have been executed and follows the Last In First Out (LIFO) principle. We use call stack for memorizing which function is running right now. The below example demonstrates the call stack.
function f1() {
console.log('Hi f1!');
}
function f2() {
f1();
console.log('Hi f2!');
}
f2();
Output:
Hi f1!
Hi f2!
The steps and illustrations below explain the call stack of the above function.
Step 1 : As webpage load it creates global callstack.
Step 2 : f2() added to callstack as it called in program.
Step 3 : f2() called f1() so f1() added to callstack.
Step 4 : f1() returned the result and callstack poped it.
Step 5 : f2() returned the result and callstack poped it also.
Step 6 : callstack now have only global callstack
Hoisting in Javascript
Hoisting refers to the process whereby the interpreter appears to move the declaration of functions, variables or classes to the top of their scope, prior to execution of the code.
console.log(a);
f1();
let a= "hello";
function f1(){
console.log("Hey, How are you!");
}
in above case we can see that variable a
and function f1()
have been called before its initialization.
for the same the output would be
console.log(a); // undefined error
f1(); // "Hey, How are you!"
let a= "hello";
function f1(){
console.log("Hey, How are you!");
}
as we can see now how variable instead of reference error showing undefined which means that variable is present in code but value is not assigned to same.