Top javascript interview questions and answers


What is JavaScript?

JavaScript is the most popular web scripting language. It is lightweight, interpreted programming language with object-oriented capabilities. It used for both client side as well as server side development.


What are the features of JavaScript?

Some important features of JavaScript are:

  • JavaScript is lightweight, interpreted programming language.
  • It is an open, cross-platform scripting language.
  • It easily integrates with backend programming language.
  • It is designed especially for network centric applications.

What are the advantages of using JavaScript?

Here are some important advantages of JavaScript:

  • Less server interaction – You can validate a form before sending it to the server.
  • Immediate feedback to the visitors – Visitors get immediate error or response for anything they missed to enter.
  • Increased interactivity – You can create interface that show some action on hover, click and many events using JavaScript.

Is JavaScript a case-sensitive language?

Yes. JavaScript is a case-sensitive language.


List some built-in methods in JavaScript?

Following are some of the built-in methods used in JavaScript:

  • anchor() – Creates an HTML anchor to be used as a hypertext target.
  • concat() – Combines two strings and returns single string.
  • Date() – Returns the present date and time.
  • filter() – Creates new array with all the elements of an array for which the filtering function returns true.
  • match() – Used for matching a regular expression pattern against a specified string.
  • ceil() – Returns the smallest integer from the given number. (It can be greater than or equal to the number provided).
  • constructor() – Returns a function along with the instantiation of the object.
  • exec() – Searches for a match in the string parameter.
  • pop() – Removes and returns the last element of an array.
  • round() – Round off the value of a number to its nearest integer.
  • some() – Returns true if atleast one element of an array satisfies the given testing function.
  • sup() – Displays a string as superscript.
  • toUpperCase() – Change a text to uppercase.

What’s the difference between undefined and null?

Undefined is the default value of a variable that has not been yet assigned a value. In other words, a variable that has been declared, but not defined.

You can explicitly set a variable value to equal undefined:

let a = undefined;
console.log(a); //undefined

Any non-existent properties in an object returns undefined:

var c = {};
console.log(c.cake); //undefined
let _varUndefined;

const someObject = {
a:"12",
b:"39",
c:"50"
};

console.log(_varUndefined); //logs Undefined
console.log(someObject["d"]); //logs Undefined

null is a non-existent value that represents no value. Null must be assigned.

let c = null;
console.log(c); //null

Some important points:

  • null is an assigned value. It means empty or nothing.
  • undefined means a variable is declared but not defined.
  • null is an object. undefined is of type undefined.

What is the DOM?

DOM stands for Document Object Model. It is an interface for HTML and XML documents. The DOM is used for interacting and modifying the structure of DOM or a particular elements of nodes.

The DOM in JavaScript provides you many methods that you can use to select elements.


What are the different types of Error Name values in JavaScript?

Following are the 6 types of Error name values.

  • Eval Error – This type of errors thrown when coming across an error in eval().
  • Range Error – Error occurred when a number is used outside of the specified range is used.
  • Reference Error – It occurred when an undeclared variable is used.
  • Syntax Error – Error occurred when incorrect syntax is used.
  • Type Error – When a value outside the range of data types is used, it thrown type error.
  • URI Error – Thrown when use of illegal characters.

What is Self Invoking Function and its syntax?

Functions that are invoked automatically is termed as Self Invoking Functions. These types of function are also called Self Executing Anonymous Functions.

Syntax of Self Invoking Function is :

(my_function() {
return() 
})();

What is the difference between “var” and “let” Keywords?

The main difference in Var and Let is the scope difference. Var is function scoped and let is block scoped.

Varlet
It was introduced with JavaScriptThe let keyword was added in ES6.(2015) version of JS.
It has global scope.Limited to block scope.
It can be declared globally and can be accessed globally.can be declared globally but cannot be accessed globally.
It is hoisted.not hoisted.

Var Example:

function varGreeting()
{
var x = 10;
var x = 20; //x is replaced
console.log(x);
}
varGreeting();

let Example:

function letGreeting()
{
let x=10;
let x=20; //SyntaxError
//Identifier x is already been declared
console.log(x);
}
letGreeting();

What is the purpose of ‘this’ operator in JavaScript?

“this” keyword in javascript refers to an object, which is executing the current context.


What is callback?

A callback is a javascript function passed to some method as an argument. It executed after another function has finished executing.


Why do we need Callbacks?

There is a very important reason, to use callback. JavaScript is an event driven language. This means JavaScript keep executing even if response is waiting.

Let’s understand by example –

function doSchoolwork(subject, callback)
{
alert("Starting my ${subject} for Home work.);
callback();
}

function taskComplete()
{
alert("Finished my all homework!");
}

doSchoolwork('math', taskComplete);

Trending Topics