Common JavaScript Interview Questions

Published Jan. 23, 2019

Looking to start a JavaScript career? This guide can help you nail some of the most essential interview questions in JavaScript.

Common JavaScript Interview Questions

What are the advantages of using JavaScript?

Following are the advantages of using JavaScript:

  • Less server interaction − You can validate user input before sending the page off to the server. This saves server traffic, which means less load on your server.

  • Immediate feedback to the visitors − They don’t have to wait for a page reload to see if they have forgotten to enter something.

  • Increased interactivity − You can create interfaces that react when the user hovers over them with a mouse or activates them via the keyboard.

  • Richer interfaces − You can use JavaScript to include such items as drag-and-drop components and sliders to give a Rich Interface to your site visitors.

What are disadvantages of using JavaScript?

We can not treat JavaScript as a full fledged programming language. It lacks the following important features. Note: Many of these things are possible with Node.js

  • Client-side JavaScript does not allow the reading or writing of files. This has been kept for security reason.

  • JavaScript can not be used for Networking applications because there is no such support available.

  • JavaScript doesn’t have any multithreading or multiprocess capabilities.

What is callback?

A callback is a plain JavaScript function passed to some method as an argument or option. Some callbacks are just events, called to give the user a chance to react when a certain state is triggered.

What are the variable naming conventions in JavaScript?

While naming your variables in JavaScript keep following rules in mind.

You should not use any of the JavaScript reserved keyword as variable name. These keywords are mentioned in the next section. For example, break or boolean variable names are not valid.

JavaScript variable names should not start with a numeral (0-9). They must begin with a letter or the underscore character. For example, 123test is an invalid variable name but _123test is a valid one.

JavaScript variable names are case sensitive. For example, Name and name are two different variables.

What is an ECMAScript?

ECMAScript (European Computer Manufacturers Association) Script is a specification for the scripting language standards. It has standardized Javascript which made Javascript the best implementation of ECMAScript.

List out the different ways an HTML element can be accessed in a Javascript code.

Here are the list of ways an HTML element can be accessed in a Javascript code,

  • getElementById('idname'): Gets an element by its ID name

  • getElementsByClass('classname'): Gets all the elements that have the given classname.

  • getElementsByTagName('tagname'): Gets all the elements that have the given tag name.

  • querySelector(): This function takes css style selector (like #id/.classname/tagname) and returns the first selected element.

  • querySelectorAll(): Similar to querySelector, this function returns a NodeList of html elements.

In how many ways can Javascript code be involved in an HTML file?

The Javascript code can be involved in 3 ways (i) Inline (ii) Internal (iii) External

What are the new ways to define a variable in Javascript?

There are three possible ways of defining a variable in Javascript (i) var (ii) const (iii) let. The last two ways are the latest ways of defining a variable and are introduced in ES-2015(ES6 version)

What does a typeof operator do?

The operator typeof gives the type of a variable/data available in it.

What is the difference between Local Storage and Session Storage?

Local Storage will stay until it is manually cleared through settings or program.

Session Storage will leave when the browser is closed.

What is the difference between the operators ‘==’ and ‘===’?

The operator ‘==’ compares the value; whereas, the operator ‘===’ compares both value and type.

What is the difference between null and undefined?

When used the typeof operator on null; i.e., typeof(null), the value is an object. Whereas, when used the typeof operator on undefined; i.e., typeof(undefined), the value would be undefined.

What are self Executing Functions?

These functions are executed right after its definition. Also called as Immediately Invoked Function Expressions (IIFE’s). Syntax:

(function(){
    console.log('in iffe');
})()

Explain the terms synchronous and asynchronous code.

The synchronous code is something that should be finished before anything else can happen, or in other words, the synchronous code is blocking. And the Asynchronous code is something in which actions can happen and is not dependent on other actions- in other words, it is non-blocking.

What is the use of the ‘this’ keyword?

The keyword ‘this’ refers to the current instance of the object when used inside a function. But, when used outside a function, it refers to the window object.

What is the difference between typeof and instanceof operators in Javascript?

The typeof operator returns a string of what type the operand is. Whereas, the instanceof operator does not work with primitive data types; but works with objects and checks on what type the object is.

What is the difference between window and document in Javascript?

Javascript window is a global object which holds variables, functions, history, location; the document also comes under the window and can be considered as the property of the window.

When do we use JSON.stringify()?

The JSON.stringify() method is used to convert a Javascript data to a string.

Sources

https://www.greycampus.com/blog/programming/top-javascript-interview-questions-and-answers https://www.tutorialspoint.com/javascript/javascript_interview_questions.htm