JAVASCRIPT Interview Questions and Answers

JavaScript often abbreviated as JS, is a lightweight, high level, interpreted or JIT compiled programming language with first-class functions. It is a client-side scripting language.Most well known as scripting language for Web pages. It is based on ECMAScript.

1) List some data types supported by Javascript?

Here is the list of data types supported by Javascript :- Undefined,Null,Boolean,String,Symbol,Number,Object.

2) What is the difference between let and var?

Both var and let are used for variable/ method declaration in javascript but the main difference between let and var is that var is function scoped whereas let is block scoped.

3) Explain Closures in JavaScript?

Closures are the combination of lexical environment and function within which the function was declared. This allows JavaScript programmers to write better, more creative, concise and expressive codes. The closure will consist of all the local variables that were in-scope when the closure was created. Sure, closures appear to be complex and beyond the scope, but after you read this article, closures will be much more easy to understand and more simple for your everyday JavaScript programming tasks. JavaScript is a very function-oriented language it gives the user freedom to use functions as the wish of the programmer.

4) Explain JavaScript Event Delegation Model?

In JavaScript, there is some cool stuff that makes it the best of all. One of them is Delegation Model. When capturing and bubbling, allow functions to implement one single handler to many elements at one particular time then that is called event delegation. Event delegation allows you to add event listeners to one parent instead of specified nodes. That particular listener analyzes bubbled events to find a match on the child elements. Many people think it to be complicated but in reality, it is very simple if one starts understanding it.

5) Explain function hoisting in JavaScript?

JavaScript’s default behavior that allows moving declarations to the top is called Hoisting. There are  2 ways of creating functions in JavaScript :

  1. Function Declaration
  2. Function Expression 

 Function Declaration: A function with the specific parameters is known as function declarations. To create a variable in JavaScript is called declarations.

Example : function hoisted() { console.log('foo'); };   hoisted(); // logs "foo"

Function Expression: When a function is created by using an expression it is called function expression.

Example : var notHoisted = function() { console.log('bar'); };     notHoisted(); // TypeError: notHoisted is not a function

6) What is the use of let & const?

In modern javascript let & const are different ways of creating variables.Earlier in javascript, we use the var keyword for creating variables. let & const keyword is introduced in version ES6 with the vision of creating two different types of variables in javascript one is immutable and other is mutable. const: It is used to create an immutable variable. Immutable variables are variables whose value is never changed in the complete life cycle of the program. let: let is used to create a mutable variable. Mutable variables are normal variables like var that can be changed any number of time.

7) Explain Arrow functions?

An arrow function is a consise and short way to write function expressions in Es6 or above.A rrow functions cannot be used as constructors and also does not supports this, arguments, super, or keywords. It is best suited for non-method functions. In general an arrow function looks like const function_name= ()=>{}

Example: const greet=()=>{console.log('hello');} greet();

8) Explain “use strict” ?

“use strict” is a javascript directive that is introduced in Es5. The purpose of using “use strict” directive is to enforce the code is executed in strict mode. In strict mode we can’t use a variable without declaring it. “use strict” is ignored by earlier versions of Javascript.

9) How to get the last index of a string in Javascript?

string.length-1 is used to get the last index of a string in Javascript.

Example :- var myString="JavascriptQuestions"; console.log(myString.length-1);

10) How to get the primitive value of a string in Javascript?

In Javascript valueOf() method is used to get the primitive value of a string.

Example : var myVar= "Hi!" console.log(myVar.valueOf())