Javascript-Interview-Questions

JavaScript Interview Questions

> Click :star: if you like the project. Pull Request are highly appreciated. Follow me [@iamkoushik1999](https://twitter.com/iamkoushik1999) for technical updates.

Table of Contents

No. Questions
1. What are the different data types present in javascript ?
2. What is the difference between Undefined & Null ?
3. Difference between “ == “ and “ === “ operators.
4. Difference between var and let and const keyword in javascript.
5. What is the NaN property in JavaScript?
6. Explain “this” keyword.
7. Explain Hoisting in javascript.
8. Explain Closures in JavaScript.
9. What is callback?
10. Explain Promise in Javascript.
11. Explain Async & Await in Javascript.
12. What is recursion in a programming language?
13. What is DOM?
14. What are arrow function?
15. Differences between declaring variables using var, let and const.
16. What is an Array?
17. How to write Functions in JavaScript?
18.

⬆ Back to Top

  1. What are the different data types present in javascript ?

    Primitive types

    1. String
    2. Number
    3. Big Int
    4. Boolean
    5. Undefined
    6. Null
    7. Symbol

⬆ Back to Top

  1. What is the difference between Undefined & Null?

Undefined NULL
The undefined property indicates that a variable has not been declared at all. The value null represents the absence of any object value

Undefined Example

var demo;
alert(demo); //shows undefined
alert(type of demo); //shows undefined

NULL Example

var demo = null;
alert(demo); //shows null
alert(typeof demo); //shows object

When to use null object?

Can we assign undefined to variable?

So the summary is undefined is #javascript way of saying that you forgot to initialize something or it is not existent (assigned by js) and null is user way of saying that I have initialized and want the variable to be non existent (assigned by user).

⬆ Back to Top

  1. Difference between “ == “ and “ === “ operators.

| == | === | | —————————————————————————————————- | ———————————————————————————————– | | returns true if operands have the same data type and same value, returns false if the values differ. | returns true only if operands are of the same data type and same value, otherwise returns false | | Also known as loose equality | Also known as strict equality |

== Example

var x = 2;
x == y; // Returns true since the value of both x and y is the same

=== Example

var y = "2";
x === y; // Returns false since the typeof x is "number" and typeof y is "string"

⬆ Back to Top

  1. Difference between var and let and const keyword in javascript.

var let const
The scope of a var variable is functional scope. The scope of a let variable is block scope. The scope of a const variable is block scope.
It can be updated and re-declared into the scope. It can be updated but cannot be re-declared into the scope. It cannot be updated or re-declared into the scope.
It can be declared without initialisation. It can be declared without initialisation. It cannot be declared without initialisation.
It can be accessed without initialization as its default value is “undefined”. It can be accessed without initialization as its default value is “undefined”. It cannot be accessed without initialisation, as it cannot be declared without initialisation.

⬆ Back to Top

  1. What is the NaN property in JavaScript?

NaN property represents the “Not-a-Number” value. It indicates a value that is not a legal number.

isNaN("Hello"); // Returns true
isNaN(345); // Returns false
isNaN("1"); // Returns false, since '1' is converted to Number type which results in 0 ( a number)
isNaN(undefined); // Returns true

⬆ Back to Top

  1. Explain “this” keyword.

The “this” keyword refers to the object that the function is a property of. The value of the “this” keyword will always depend on the object that is invoking the function.

var obj = {
  name: "Vivek",
  getName: function () {
    console.log(this.name);
  },
};
obj.getName();

⬆ Back to Top

  1. Explain Hoisting in javascript.

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, before execution of the code.

Hoisting allows functions to be safely used in code before they are declared.

Without Hoisting

function catName(name) {
  console.log(`My cat's name is ${name}`);
}

catName("Tiger");
/*
The result of the code above is the same: "My cat's name is Tiger"
*/

With Hoisting

catName("Tiger");

function catName(name) {
  console.log(`My cat's name is ${name}`);
}
/*
The result of the code above is: "My cat's name is Tiger"
*/

⬆ Back to Top

  1. Explain Closures in JavaScript.

A closure is simply a function inside another function with access to the outer function variable.

In other words, closure is created when a child function keeps the environment of the parent scope even after the parent function has already been executed

closures image

⬆ Back to Top

  1. What is callback?

In javascript, a callback is simply a function that is passed to another function as a parameter and is invoked or executed inside the other function.

callback image

⬆ Back to Top

  1. Explain Promise in Javascript.

We understand the concept of callback but what will happen if your code will have callbacks within callbacks within callbacks and it goes on. Well, this recursive structure of callback is called ‘callback hell’ and promises to help to solve this kind of issue. Promises are useful in asynchronous javascript operations when we need to execute two or more back-to-back operations (or chaining callback), where each subsequent function starts when the previous one is completed. A promise is an object that may produce a single value sometime in the future, either a resolved value or a reason that it’s not resolved (rejected)

A promise may be in three possible states…

We can consume any promise by attaching then() and catch() methods to the consumer.

promises image

⬆ Back to Top

  1. Explain Async & Await in Javascript.

Stop and wait until something is resolved. Async & await just syntactic sugar on top of Promises and like promises it also provides a way to maintain asynchronous operation more synchronously. So in javascript asynchronous operations can be handled in various versions…

You can use Async/Await to perform the Rest API request where you want the data to fully load before pushing it to the view. For Nodejs and browser programmers async/await is a great syntactic improvement. It helps the developer to implement functional programming in javascript and it also increases the code readability.

⬆ Back to Top

  1. What is recursion in a programming language?

Recursion is a technique to iterate over an operation by having a function call itself repeatedly until it arrives at a result.

⬆ Back to Top

  1. What is DOM?

⬆ Back to Top

  1. What are arrow function?

let myFunction = (a, b) => a * b;

⬆ Back to Top

  1. Differences between declaring variables using var, let and const.

Keyword const let var
global scope no no yes
function scope yes yes yes
block yes yes no
can be reassigned no yes yes

⬆ Back to Top

  1. What is an Array?

An array is a collection of the same data type elements

⬆ Back to Top

  1. How to write Functions in JavaScript?

There are 4 ways to write Functions in JavaScript

// Function Declaration
function square(x) {
  return x * x;
}

// Function Expression
const square = function(x) {
  return x * x;
}

// Arrow Function Expression
function square =(x)=> {
  return x * x;
}

// Concise Arrow Function Expression
function square = x => x * x;

⬆ Back to Top