Javascript Interview Questions and Answers



1.What are the different datatypes are present in javascript?
Primitive-types:

  1. String
  2. Number
  3. BigInt
  4. Boolean
  5. Undefined
  6. Null
  7. typeof

Non Primitive-types:

  1. Object
 2. Explain Hoisting in javascript.

Hoisting is a default behaviour of javascript where all the variable and function declarations are moved on top. 
 
 3.Difference between “ == “ and “ === “ operators

== This operator will compares the values.

=== This operator will compares the values and data types

4.Is javascript a statically typed or a dynamically typed language?

JavaScript is a dynamically typed language. In a dynamically typed language, the type of a variable is checked during run-time in contrast to statically typed language, where the type of a variable is checked during compile-time. 
 
5.What is NaN property in JavaScript?
  1. NaN property represents “Not-a-Number” value. It indicates a value which is not a legal number.
  2. typeof of a NaN will return a Number .
  3. To check if a value is NaN, we use the isNaN() function, 

6.Explain “this” keyword.

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

7.What is promises?

Promises are used to handle asynchronous operations in JavaScript. They are easy to manage when dealing with multiple asynchronous operations where callbacks can create callback hell leading to unmanageable code.

8. What is callback?

Callback is a function that is passed as an argument to an another function. A callback function can run after another function has finished

9.What is Call method?

This method invokes a method (function) by specifying the owner object. This will takes arguments separately.

10.What is apply method?

This is similar to call method. But this will takes arguments as an array 

11.What is bind method?

  1. This is nothing but function borrow method.
  2. with the bind() method the object will borrow the function from other object

12.Explain Scope and Scope Chain in javascript. 

Scope in JS, determines the accessibility of variables and functions at various parts in one’s code.

In general terms, the scope will let us know at a given part of code, what are the variables and functions that we can or cannot access.

There are three types of scopes in JS:

  • Global Scope
  • Local or Function Scope
  • Block Scope

Global Scope:

Variables or functions declared in the global namespace have global scope, which means all the variables and functions having global scope can be accessed from anywhere inside the code  

Local or Function Scope: 

Any variables or functions declared inside a function have local/function scope, which means that all the variables and functions declared inside a function, can be accessed from within the function and not outside of it.

 Block Scope:

  1. Block scope is related to the variables declared using let and const. Variables declared with var do not have block scope. 
  2. Block scope tells us that any variable declared inside a block { }, can be accessed only inside that block and cannot be accessed outside of it.    
13.What is asynchronous in javascript?
Functions running in parallel with other functions are called asynchronous.
Eg: setTimeOut()
 
14.What is async in javascript?
The word “async” before a function means one simple thing: a function always returns a promise. Other values are wrapped in a resolved promise automatically.
 
15.What is await in javascript?
The syntax:
// works only inside async functions
let value = await promise;

The keyword await makes JavaScript wait until that promise settles and returns its result.
Here’s an example with a promise that resolves in 1 second:
async function f() {

  let promise = new Promise((resolve, reject) => {
    setTimeout(() => resolve("done!"), 1000)
  });

  let result = await promise; // wait until the promise resolves (*)

  alert(result); // "done!"
}

f();
 16.What is DOM?
With the HTML DOM , Javascript can access and change all the elements of an HTML elements.
When a web page loaded, the browser creates Document Object Model of the page

 17.What is arrow function?
Arrow function used to write shorter function syntax. It is introduced in ES6
let myFunction = (a,b)=>a*b;

18.What is closures?

Closures is an ability of a function to remember the variables and functions that are declared in its outer scope. 
Closures are created whenever a variable that is defined outside the current scope is accessed from within some inner scope. It gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created. To use a closure, simply define a function inside another function and expose it.
 
19.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. 

20.What are generator functions?
  1. Introduced in ES6 version, generator functions are a special class of functions.  
  2. They can be stopped midway and then continue from where it had stopped.

 21.Name some of the built-in methods and the values returned by them.

Built-in MethodValues
CharAt()It returns the character at the specified index.
Concat()It joins two or more strings.
forEach()It calls a function for each element in the array.
indexOf()It returns the index within the calling String object of the first occurrence of the specified value.
length()It returns the length of the string.
pop()It removes the last element from an array and returns that element.
push()It adds one or more elements to the end of an array and returns the new length of the array.
reverse()It reverses the order of the elements of an array.

 

 22.What is the difference between Local storage & Session storage?

Local Storage – The data is not sent back to the server for every HTTP request (HTML, images, JavaScript, CSS, etc) – reducing the amount of traffic between client and server. It will stay until it is manually cleared through settings or program.

Session Storage – It is similar to local storage; the only difference is while data stored in local storage has no expiration time, data stored in session storage gets cleared when the page session ends. Session Storage will leave when the browser is closed.

23.What is the difference between undeclared & undefined?

Undeclared variables are those that do not exist in a program and are not declared. If the program tries to read the value of an undeclared variable, then a runtime error is encountered. Undefined variables are those that are declared in the program but have not been given any value. If the program tries to read the value of an undefined variable, an undefined value is returned.

24.

 

 

 

Comments