Front End Interview Series-Part 1

Front End Interview Series-Part 1

A Front End Interview guide to help you prepare for your next interview. It is curated from recently asked interview questions.

Q1: How does JavaScript work?

JavaScript is a single-threaded, synchronous language which executes one thing at a time, line by line. The three key components that help in execution of JavaScript are: Call Stack, Event loop and Callback Queue. Whenever an asynchronous code is encountered it is run by the browser and in the mean time JavaScript continues to run the rest of the code. As soon as the browser is finished, the result is pushed to the callback queue, which works on the queue data structure’s mechanism, first-in first-out, that is, whatever is pushed into it first will be the first one to get out. Event loop keeps track of the call stack, whenever the call stack is empty, the first item from the callback queue is pushed onto call stack and gets executed.

Q2: What is the difference between let, var and const?

In JavaScript, variables can be declared using let,var and const keywords. The variables created using var keyword are functional scoped. Functional scope means anything which is inside a function.On the other hand, variables declared with let and const are block scoped and are accessible only inside the block they are declared in. Block is accumulating multiple statements inside curly braces { }, for example loops and conditional statements.

Q3: What is the difference between undefined and null?

undefined and null both are primitive types in JavaScript. undefined means a value does not exist in the scope. null means the value is made unavailable intentionally. The type of undefined is equal to undefined and the type of null is an object.

Q4: What will be the output of the code below and why?

console.log(a);
var a="Hello";
console.log(b);
let b=10;

The first console will give undefined and another one will give Reference Error: Cannot access ‘b’ before initialisation. The reason behind this is how JavaScript executes the code. There are two phases of execution:creation phase and execution phase. In creation phase JavaScript allocates memory to its variables and functions and for variables declared with var, JavaScript initialises them with undefined and that’s why when we log “a” before its initialisation, its value is undefined. In case of let and const declared variables, they are initialised at a different memory location and are not accessible to us before their initialisation, this is called “temporal dead zone”. And that’s why JavaScript gives us a reference error in this case.

Q5: What is Hoisting?

As we saw in the previous question, we were able to access ‘a’ variable before it was declared, irrespective of the value it gives. This behaviour is called Hoisting in JavaScript. Variables can be initialised before they are even declared in JavaScript. But for let and const, we get an error doing so because they lie in the temporal dead zone which is not accessible to us unless we declare them. So they are hoisted too but in a different memory location.

Q6: What is the difference between == and === ?

These two operators are used to perform equality in JavaScript. == is also referred as “loose equality” because it performs type coercion before making the comparison, which means if the two values are of not the same type this will first convert the type so that they both have the same type and them compare their value and type. On the other hand, === is referred to as “strict equality” because it does not perform type coercion and compares the type and values as it is. To avoid unnecessary bugs, we must prefer using ===.

Q7: Why does it return false when comparing two similar objects in JavaScript?

In JavaScript, objects are compared by reference which means whenever we compare any two objects we are not comparing their values but only the reference to the memory location. JavaScript checks whether they point to the same location or not and since each new object will have a different location we get the result as false.

Q8: What is the difference between a function expression and function declaration?

When a function is created using ‘function’ keyword and not assigned to any variable, that is called function declaration. For example:

function declaredFn(){...}

On the other hand, when a function is assigned to a variable,then it is called function expression. For example:

var expression=function fnExpression(){...}

Q9: Why are functions called first class objects?

Functions in JavaScript can be passed as an argument to another function, assigned to a variable and returned from another function. Due to these abilities functions in JavaScript are called first class objects.

Q10: What is the difference between an argument and a parameter?

When we are declaring a function, the variables we put in between the parenthesis (), are called function parameters. On the other hand, when we are calling a function, the values passed in between the parenthesis are called arguments which eventually are assigned to the parameters of the function.

Did you find this article valuable?

Support Sakshi Chaudhary's Blog by becoming a sponsor. Any amount is appreciated!