Javascript interview Preparation

Fardin Khan
5 min readMay 8, 2021

1.Truthy, falsy value

Truthy and falsy value come when we use if else type of thing in javascript

there are some types which are always falsy and the rest is truthy
.but it also depends on the condition but in this case we only talk about the truthy and falsy value

let a = 1;
if(a){
console.log(“its a truthy value”)
}
else{
console.log(“its a falsy value”)
}

here 1 is a truthy value so it will appear as truthy but if you give 0 then it will be falsy.
then if you give ‘’ empty string it will also show false
here are the list of falsy value

0,’’,null,false,undefined,NaN

let a = NaN;
if(a){
console.log(“its a truthy value”)
}
else{
console.log(“its a falsy value”)
}

2.Double equal (==) vs Triple Equal (===)

double equal and triple equal both match the two value if they are equal or not.
but the major difference is double equal only check the value where triple equal check the value and type both

let’s understand with an example

let a = 10 ;
let b = “10” ;

if (a == b) {
console.log(“ it’s a true condition “)
}
else {
console.log(“it’s a false condition “)
}

if you run this code the result will be true side. but the a is number type where the b variable is string type. But there value is same so it will identify them as a true condition

but of you use === instead of == than see what happen

let a = 10 ;
let b = “10”;

if (a == b) {
console.log(“ it’s a true condition “)
}
else {
console.log(“it’s a false condition “)
}

now the result will be shown as false. so basically the three equal compare both type and value where the two equal only compare the value

3.Scope, Whesting

in javascript scope is somethings area from where the variable or function or object can be used.

let and const are block scope where if you use var for define variable then we can do westing.

let’s explain it with a example

function addition (){
let a = 5 ;
let b = 10;

console.log(a+b);

}

console.log(a)

in this code we called a variable outside of the function for this it will show an error because it’s outside of it’s scope. This will happen for the let and const. We call it block scope which meaning is it will on work under it’s scope can’t go to upper

but instead of using let or const if we use var then see what happends

function addition (){
var a = 5 ;
var b = 10;

console.log(a+b);

}

console.log(a)

this time you will see the result of a. In this case var can be work outside of the scope we call it whesting or global scope

4.Closure

When you have function in js and you return antoher function under that function then it will make a closed environment where you can use the same function as a variable and again if you assign the same function into another variable then you will get separate values.

function stopWatch(){
let count =0;
return function counts (){
count ++;
console.log(count);
}

var clock1 = stopWatch()

var clock2= stopWatch()

console.log(clock1())
console.log(clock1())
console.log(clock1())
console.log(clock2())

here in the first clock1 it will increase by it self but it’s increment don’t have any effect on the 2nd clock2. This is what is closure. It’s a closed environment where we can use the same functions for different purpose

5.Window, global variable and global scope

In javascript window is where javascript is executed. It’s the top level of DOM tree. Window is the global scope.

global scope and global variable is something what can be call be anywhere on the document.

example:
var name =”fardon”;
function add (num1,num2){

var result = num1+num2;

console.log(name)
return result ;

}
var c = add(2,3);
console.log(result)

here you will get an error because result can’t be called from outside of the function.It’s in a local scope but the name variable can be call from anywhere of the window because it’s a global variable in global scope

you can make the result global variable by adding window.result
. It will make it available in global scope

6.Understanding the this keyword

If you call this keyword from inside of an object then it will mean that object. example

const anObj = {
name : “jhankar vai “,
getFullName : function(){
console.log(this)
return this.name

}
}

console.log(anObj.getFullName)

here if you run this code you will see the whole object then the name property. and we use this keyword inside getFullName function. so this keyword actually mean that object or function etc. but there are some exceptional case.

so this keyword will be define by its calling context

7.How js works, Event loop stack and queue

When we call any function of js it will work with an event loop. When a event loop is started it will have a stack of works which have to done that time and there will be also tasks what js will do later.

These wating task will be in queue. then js will do works from stack from the upper one by one and after completing it than it will start the queue works

8.Null vs undefined and how you will get a undefined or null value

If you just declare an function but don’t give any value than it will be undefine.

let a ;

here a will be undefine ;

then if you don’t return a value from function it will show undefine in that case

function add(n1,n2){
console.log(n1,n2)
}

console.log(add(2,3))

here you will see undefine because this function don’t return anything

again if you don’t give a input it will show undefine

like add(2) so here n2 will be undefine

or if you want to read any property from a object which is not available that will also give a undefined value

and null is when developer set it wishly.

var a = null;

9.What is an API. api purpose get, post

A api is an application programming interface and a procedure by what we verify something and then give access.

API is basically a a set of data what we give to other or take from other for various purpose and it will be safe by a authentication and by api we can take and give only limited and needed data.

There are various method in API. by we can use it. Like GET method we can get data from api, Then with POST method we can post data into api and there are also delete method so that we can delete data

10.What is DOM

DOM mean Document Object Model. It’s basically a way how we manupulate a web page. We see the full web page as a DOM in js so that we can do our work.

In javascript we think the whole webpage as a tree model and every tag of html is a object here. so that we can manupulate these thing with js. You hered about CRUD method. which means create, read,update and delete

for this dom model the whole web page is like a tree model and we can do crud to any object

--

--