JavaScript Important Interview Questions

Level - Basic

1. What is JavaScript?

JavaScript (JS) is a light weight client side programming language. By client side we mean JS is executed in client side (i.e. Browser) and not in server. JS is used to make web pages client side dynamic like showing dynamic content in web pages, showing error/alert messages to user based on their input in forms (form validation), animating elements of web pages, dynamically changing style and so on. You can say JS is like the brain of a web page.

2. Is JavaScript case sensitive?

Yes, JavaScript is case sensitive. For example, user & User are two different variables in JavaScript.

3. What is the use of window.location object?

The window.location object provides current page address (URL) and it can be used to redirect the browser to a new page.

4. What is the use of window.location.href?

The window.location.href returns the href (URL) of the current page.

5. What is the use of history.back() method?

The current history.back() method redirects back to previous page (same as clicking back in the browser).

6. What is the use of return statement?

When a return statement is used in a function body, the execution of the function is stopped. If specified, a given value is returned to the function caller.

function getMessage() { 
    let message = 'Hello!';
    return message;
}
    
function divide(x,y) { 
    if(y==0) return; // Stop execution here if y=0
    let result = x/y;
    return result;
}
7. Can you call two functions on button click?

Yes, we can call two functions on a button click. Define two functions and call the first function inside second function. On button click call the second function.

func1(){
  // body
}
func2(){
  // body
  func1();
}

<button onclick="func2()">Call</button>

8. What are different data types available in JavaScript?

In JavaScript, as per latest ECMAScript standard there are nine different Data Types which can be grouped into three broad categories –
Primitive or Primary - undefined, Boolean, Number, String, BigInt and Symbol
Non-data Structure or Composite – Object and Function
Special – null

For more details read this article:

Data Types in JavaScript
9. What is the difference between == and === operators?

The strict equality operator (===) behaves identically to the abstract equality operator (==) except no type conversion is done, and the types must be the same to be considered equal.

false == '0'; // true 
false === '0'; // false

10. What is IIFE?

An IIFE (Immediately Invoked Function Expression) pronounced as iify is a JavaScript function that runs as soon as it is defined. In other words, the function gets executed automatically as soon as the page loads. There is no need to call this function anywhere in the script. JavaScript engine directly interprets the function.

For more details read this article:

What is IIFE in JavaScript
11. What would be the output of 3+2+"7"?

The output would be 57. In the expression "7" is string. JavaScript will sum up the numbers 3+2=5 and concat it with "7". So 3+2 = 5 and 5+"7" = 57

12. How do you create an Object in JavaScript?

In JavaScript, objects can be created as follows:


let obj = {
   name:"Lokesh", 
   age:"30", 
   profession: "Developer"
}
                         

Object consist of key/value pairs. The object obj in the above example has three keys - name, age and profession.