10X Sale
kh logo
All Courses

Introduction

JavaScript is a high-level, dynamic, object-oriented programming language that adds interactivity and functionality to web pages, allowing users to interact with web content in a more dynamic and responsive way. Apart from Web Development, it is also used for server-side programming and desktop application development. You can stay in the know with the frequently asked JavaScript interview questions with answers that will help you crack your next JavaScript interview and land your dream career as a JavaScript Developer.

We have compiled questions on important JavaScript topics such as Closure, Event bubbling, Capturing, and Template Strings in ES6, meant for beginners, intermediate and expert professionals at the same time. Take your JavaScript proficiency to the next best level well with these expert-curated JavaScript interview questions and answers. Let us get started with the questions and answers.

JavaScript Interview Questions and Answers
Beginner

1. Is JavaScript an interpreted language or compiled language?

Let’s first understand what is an compiled language and interpreted language.

  • Compiled Languages are languages in which we turn a program first from human-readable format to machine format, generally through a compiler. After that we execute that code. So, it is generally a two-step process. Languages like C, C++ and Java are example of Compiled languages. For example if we have a “C” program(hello.c), we will first convert it into machine code by using a gcc(gnu C compiler) like below:
gcc hello.c -o hello

If no error it will generate an executable “hello” and we will run it by:

./hello
  • Interpreted Languages are languages in which code doesn’t needs to be compiled first. Language like Perl, Python are interpreted languages. To run a python code we just need to run it directly by command like below:
python hello.py

The above code does not need to be compiled first but it does require that python is installed on any machine that needs to run the script.

  • JavaScript is a special case where you directly execute your source code. A webpage will directly execute your JavaScript. So, for that reason many people think JavaScript as a interpreted language.

However there is a compilation step just before the interpretation step in JavaScript. So, JS is both compiled and interpreted language.

  • Compilation Step – During this step the compiler mainly registers the variable declarations.

Let consider the below example. The compilation steps mainly looks at the var keyword. It is not bothered with what is assigned in these variables.

var a = 10;
var b = 20;
console.log(a+b)

Example for  Compilation Step

When the compiler goes to line 1, it encounters var a and registers it in the global scope and then goes to line 3 and registers the var b. Line 5 is only a console and it doesn’t finds any var, so don’t do anything.

  • Interpretation Step – During this the actual execution takes place. 

For the above example, the interpreter starts at line 1 and see a variable a and ask the compiler, if it have a variable “a” in Global scope and the compiler have it. So, it assigns the value 10 to it. Next the same step is repeated for line 3 and interpreter assigns 20 to variable “b”. 

Now once the interpreter goes to line 5, it finds console. It first looks for console at global scope from the compiler but don’t find it. So, it checks in the JavaScript global and finds it. Inside the console there are variable a and b, which it finds at global scope. It then adds them using addition operator and display the result.

2. What is the difference between keyword let, const and var ?

The variable “var” was since the beginning of JavaScript i.e. since 1997. So, if someone didn’t updated there browser since the beginning(or past 10 years) the keyword “var” will only work on there browser. 

The variables "let” and “const” were introduced recently in ES6(In 2015).  There were some design mistakes made with “var” and these were rectified with “let” and “const”. 

The problem is that “var” is function scoped and it causes a lot of problems.
Consider the below example where we are using “var” inside a traditional “for” loop. But we are also able to access and update “i” outside of the “for” loop.

for(var i=0; i<5; i++) {
  console.log(i); //Output- 0 1 2 3 4
}
 
i = i + 2;
console.log(i); //Output- 7

Since, “var” is function scope so we can put our “for” loop inside a function and then “i”  won’t be accessible outside.

function incrementI() {  
  for(var i=0; i<5; i++) {
    console.log(i); 
  }
}
 
incrementI(); //Output- 0 1 2 3 4
i = i + 2;
console.log(i); 

//Output- 

/*
Exception: ReferenceError: i is not defined
@Scratchpad/6:9:1
*/

 Traditional languages like C, C++ and Java as they are blocked scope and it is what was desired from JavaScript. So, the variable “let” and “const” introduced in 2015 were made block scoped.

 The “for” loop declared with “let” will also throw an error, when we try to access “i” outside of it. It is because the scope of “i” ends with the “for” loop.

for(let i=0; i<5; i++) {
    console.log(i);  //Output- 0 1 2 3 4
}
i = i + 2;
console.log(i); 

//Output-  

/*
Exception: ReferenceError: i is not defined
@Scratchpad/6:6:1
*/

We will understand “const” now. It is similar to “let” and have block scope. But it was created to declare constant variables in JavaScript. We cannot assign a new value to a variable after the initial declaration for primitive types like integers and strings.

const c = 12;
c = 14;
console.log('c is ', c);

/*

Exception: TypeError: invalid assignment to const `c'

@Scratchpad/6:2:1

*/

But can add or update values for non-primitive like arrays and objects. 

const arr = [1, 2, 3];
arr.push(4);
console.log('arr is ', arr); // [ 1, 2, 3, 4 ]
 
const obj = { name: 'Robin', skill: 'JS' };
obj.skill = 'React';
obj.profession = 'Developer';
console.log('obj is ', obj); 
// { name: "Robin", skill: "React", profession: "Developer" }

This is one of the most frequently asked JavaScript interview questions for freshers in recent times.

3. What is the difference between == and === ?


JavaScript has both strict and type-converting equality comparison. For strict comparison we use === and for type-converting comparison we use == . Let’s look at each of it in details :

Strict Comparison(===)
For strict comparison the items been compared must be the same type.

  • Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
console.log("Coder" === "Coder"); //true
console.log("Coder" === "coder"); //false
  • Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything, including NaN. Positive and negative zeros are equal to one another.
console.log(23 === 23); //true
console.log(NaN === NaN); //false
console.log(0 === -0); //true 
  • Two Boolean operands are strictly equal if both are true or both are false.
console.log(true === true); //true
console.log(false === false); //true
  • Two objects are strictly equal if they refer to the same Object.
var obj = {};
var newObj = obj;
console.log(newObj === obj); //true
  • Null and Undefined types are not equal
console.log(null === undefined); //false
console.log(null == undefined); //true

Type-converting comparison
The == does a type conversion before comparing, if both items are of different types.

As you can see in the below example. String 1 is equal to numeric 1, when using ==. It is because the type of String 1 is converted to Number before comparison.
It is the similar case with “1 == true”. The 1 is converted into Boolean, which is true before comparison.
Same expressions are not true while using ===, as we know it doesn’t do any type conversion.

console.log('1' == 1); //true
console.log(1 == true); //true
console.log(1 === true); //false
console.log('1' === 1); //false


4. What is the difference between null and undefined ?

Both null and undefined  represents empty values. Let’s understand them first and then we will see what is the difference.

undefined
To understand “undefined” we have to understand what is declaration and definition of variables. When we declare a variable as in below diagram by “var value”, the compiler makes space for a variable “value”. The definition means that we assign a value to the variable and it’s allocated in that space. You can also do these two together by
var value = 42;

Declaration and definition

So, every language have a way of handling the value of a variable between function declaration and definition.
In JavaScript that is handled by type “undefined”. The type undefined is a primitive type which have only one value possible i.e. undefined. 

null
It is also similar to “undefined” and is a primitive type. It also have only one possible value i.e. null. It is also used to represent empty values, but is generally assigned by the users.

var a;
console.log(a); //undefined 
a = null;
console.log(a); //null

5. What is difference between function declaration and function expression ?

Function declaration is like most other traditional languages, but in JavaScript we use the keyword “function”.
In function expression we assign an anonymous function to an variable. They are very useful when we pass function as arguments to other function or return an function.

function funcDeclaration() {
  console.log('Function declaration');
}
 
let funcExpression = function() {
  console.log('Function expression');
}
 
console.log(funcDeclaration()); //Function declaration
console.log(funcExpression());  //Function expression 

One of the key difference is that, we can call a function declaration even before defining it but same is not true for function expression and it will give reference error. Let’s move both the function call to the top. 

console.log(funcDeclaration()); //Function declaration
console.log(funcExpression());  //ReferenceError
 
function funcDeclaration() {
  console.log('Function declaration');
}
 
let funcExpression = function() {
  console.log('Function expression');
}

/*

Exception: ReferenceError: can't access lexical declaration `funcExpression' before initialization

@Scratchpad/7:2:1

*/

But why did we got this Reference error in function expression call. This is to do with the compiler and interpreter step in JavaScript, which we understood in Question 1.
The compiler runs first and finds all the “var” , and the function declaration are also treated as variable declaration because in JS all function declaration are object declaration.
So, the function declaration call doesn’t give as any error.
But the same is not true about function expressions. Here when the compiler runs it registers a variable functionExpression at line 8, but it doesn’t knows what it is. So, when the interpreter runs at line 2, it throws a runtime error, because it doesn’t know what is functionExpression.

This is one of the most frequently asked JavaScript interview questions for freshers in recent times. Be careful with the expression this one.

Want to Know More?
+91

By Signing up, you agree to ourTerms & Conditionsand ourPrivacy and Policy

Description

JavaScript is a scripting language for web development. It implements complex things like displaying timely content updates, interactive maps, etc. on a webpage, making the page more lively and interactive. It is widely used in Mobile application development and Game development. JavaScript has become the most popular programming language for web development today. You can understand the importance of JavaScript from the recent reports, JavaScript is currently being used by more than 94 percent of all websites.

JavaScript is a popular product in the languages category and around 10,000 + companies are using the JavaScript software. For companies like Microsoft, Accenture, Google, Fitbit, Shopify and many more, JavaScript is immensely important. As a result, a huge number of aprirants take a JavaScript Course in order to stay ahead of the competition.

Over the last few years, JavaScript has taken over the web development world and became the primary language of choice for many new developers. It is not just because JavaScript provides a great way to make web pages interactive, but also because JavaScript developers have a great demand in the job market. The Employment of web developers is projected to grow 15 percent from 2016 to 2026, much faster than the average for all occupations. Demand will be driven by the growing popularity of mobile devices and e-commerce. JavaScript developers get $ 62,155 per month. This number can be increased when you enroll in the best web development courses.

So, if you are planning to start your career in JavaScript, you need to be well prepared with all the possible JavaScript interview questions which could be asked in a JavaScript interview. These JavaScript Interview Questions will provide you with an in-depth knowledge and help you ace your JavaScript interview.

But how can one crack a tricky JavaScript interview? How to answer the toughest of JavaScript interview questions? Don't worry, these are the questions that bother interviewees, beginners and freshers alike, more than the JavaScript interview questions themselves. These JavaScript interview questions have been designed specially to get you acquainted with the nature of questions that you may come across during your JavaScript interview.

To relieve you of the worry and burden of preparation for your upcoming interviews, we have compiled the above JavaScript interview questions with answers prepared by industry experts. These common interview questions on javascript will help you ace your Javascript Interview.

Candidates aspiring to build a career in frontend/backend web app development can learn about JavaScript from the excellent training available.

Recommended Courses

Learners Enrolled For
CTA
Got more questions? We've got answers.
Book Your Free Counselling Session Today.