19 JavaScript Questions I Have Been Asked Most In Interviews
Tags: showdev, javascript, webdev, react
Published: 2022-04-25 06:25:01

 
Being a fresher it’s a lot difficult to prepare for an interview. You will have very little idea and experience regarding their requirements, what knowledge or technical skill set do they expect from a fresher and most importantly which skill-set to apply for. In my case, I had the goal clear since the beginning, to apply for the position of a UI developer.

If you have just completed your graduation and looking to enter the front-end domain, you made the right choice. Because the domain is booming currently with new [technologies](https://www.lambdatest.com/blog/top-programming-languages-helpful-for-testers/?utm_source=devto&utm_medium=organic&utm_campaign=apr25_kj&utm_term=kj&utm_content=blog) like React, Angular and other advanced frameworks coming into play.

However, I found out that interviewers do not expect a fresher to have complete control over any such framework. They only expect us to have a strong knowledge over basic web technologies like HTML, CSS and most importantly [JavaScript](https://www.lambdatest.com/blog/15-best-books-for-javascript-beginners/?utm_source=devto&utm_medium=organic&utm_campaign=apr25_kj&utm_term=kj&utm_content=blog). Among the multiple companies where I applied, after few rejections and some selections, one thing became clear to me. If the company where you are applying is looking for professionals who can work on advanced JS frameworks, they will look for a candidate with a strong knowledge of JavaScript, the base of all those frameworks. Here we shall discuss 19 common JavaScript interview questions which I found frequently asked in interviews.

**Note:-** [**Fotot Testing**](https://www.lambdatest.com/testing-cloud/fotot-testing?utm_source=devto&utm_medium=organic&utm_campaign=apr25_kj&utm_term=kj&utm_content=testing_cloud)-Test your Fotot-created websites on different mobile and desktop devices using our high performing Device Cloud.

## What is Object and How to create it?

Everything is an object since JavaScript is an object-based language. Still, we can define an object to be an entity that has its own behavior and states.

A common method of creating an object is by creating an instance using the ‘new’ keyword.

    Var object = new Object();

## What is a Scope and How Many are There?

A scope determines how an object, a function or a variable can be accessed in a particular section of your code.

There are 2 types –

**Global**– The variable is defined outside the function and can be accessed from any section of the code.

    var name = 'LambdaTest';

    console.log(name); // logs 'LambdaTest'

    function newLogName() {

    console.log(name); // 'name' is accessible here and everywhere else

    }

    newLogName();

**Local**– Here, variables are defined inside the function and can only be accessed when called inside the function.

    // Global Scope

    function sampleFunction() {

    // Local Scope #1

    function oldFunction() {

    // Local Scope #2

    }
    }

    // Global Scope

    function newFunction() {

    // Local Scope #3

    }

    // Global Scope

## What Do You Mean by “this“?

Unlike other object-oriented programming languages where ‘this’ is an object that is instantiated by a class, in JavaScript, ‘this’ is an object which is the owner of a method.

## What is Anonymous Function?

As the name states, it is a function without a name. They are declared during runtime dynamically using a function operator since it offers more flexibility than a declarator.

    var display=function()  
    {  
     alert("Anonymous Function is declared");  
    }  
    display();

## What Do You Know About BOM?

BOM, otherwise known as the Browser Object Model serves as an interacting medium for the browser. The default object is the window, all functions can be called directly or by specifying the window. History, Screen, location, are the different properties of the Window.

## What is DOM and its Usage?

[Document Object Model](https://www.lambdatest.com/dom-matrix?utm_source=devto&utm_medium=organic&utm_campaign=apr25_kj&utm_term=kj&utm_content=blog), commonly known as DOM represents the HTML document. It is used to change the content of the HTML document.

## How To Return a Character From a Specific Index?

The charAt() method can be used to find out the value of a character at any specific index. Considering ‘n’ to be a string’s length, the index can start from 0 and end at ‘n-1’. Value of the index, however, can never be negative, equal to or greater than the string’s length.

    var str="LambdaTest";    
    document.writeln(str.charAt(4));

## Difference between “==” and “===”

This is probably the most widely asked JavaScript Interview Question.
Type converting equality (==) checks whether 2 variables are similar, irrespective of their data types. For example (“3” ==3) will return true.
Strict equality (===) checks whether 2 variables have similar data type as well as value. For example (“3” ===3) will return false.

## What are the Different Data Types in JS?

JavaScript has the following data types –

![](https://cdn-images-1.medium.com/max/2000/1*cHd3_CvSAUPxsDwKUT2ZbQ.png)

## What is Prototype Property?

Prototype property is usually used for implementing inheritance. Every function has one, by default the value of which is null. Methods and properties are added to the prototype to make it available to the instances. You can answer this JavaScript interview question with an example calculating the perimeter of a rectangle.

    function Rectangle(x, y) {

    this.x = x;

    this.y = y;

    }

    Rectangle.prototype.perimeter = function() {

    return 2 * (this.x + this.y);

    }

    var rectangle = new Rectangle(4, 3);

    console.log(rectangle.perimeter()); // outputs '14'

## What is Closure?

A function defined inside a JavaScript function is known as Closure. It can access 3 types of scopes ( Internal, outer and global). In the case of an outer function, it can also view the parameters apart from accessing the variables.

## How to Write “Hello World” in JavaScript?

This is maybe the very basic JavaScript Interview Question asked to all freshers. It can be written using the following syntax which can be placed in the body of an HTML file.
document.write(“JavaScript Hello World!”);

## How Can You Use an External JS file?

It can be done by calling the file from the HTML document using the following syntax, just like calling an external CSS file.

    <script type="text/javascript" src="custom.js"></script>

## Asynchronous Programming and Its Importance

Here, the JS engine runs in a loop of events. When a blocking operation is encountered, a request is fired and the code keeps running constantly. Once a response is ready, an interrupt is triggered. An event handler is executed, whereas the control flow continues. Thus, by asynchronous programming, a single thread can handle multiple operations simultaneously.

## Usage of Window Object

This is not a JavaScript Object but an external window created automatically by the browser. It is used to display a popup dialogue box. For example
alert() — Shows an alert box with a custom message and an ‘ok’ button.

**Note:-** **[Intel XDK Testing](https://www.lambdatest.com/testing-cloud/intel-xdk-testing?utm_source=devto&utm_medium=organic&utm_campaign=apr25_kj&utm_term=kj&utm_content=testing_cloud)** - Test your Intel XDK CSS framework based websites across 3000+ different desktop and mobile browsers.

## How different is client-side JavaScript from Server side?

Client-side JavaScript usually consists the basic language along with certain predefined objects that are relevant to the script running in the browser. Embedded directly by the HTML, it is executed during runtime by the browser.
Server-side JS is almost similar to client-side. However, it is executed in the server and deployed only after the code is compiled.

You can take this certification as proof of expertise in the field of test automation with JavaScript to empower yourself and boost your career.

Here’s a short glimpse of the Selenium JavaScript 101 certification from LambdaTest:

{% youtube DQwSRfCkhSM %}

## Why Debugging is Required in JavaScript?

This is another important JavaScript interview question. Often scenarios happen where the script does not show any error in the browser. But the output is not similar to what is expected. In that case, the best option to find out the error is by [debugging](https://www.lambdatest.com/blog/debugging-javascript-using-the-browsers-developer-console/?utm_source=devto&utm_medium=organic&utm_campaign=apr25_kj&utm_term=kj&utm_content=blog). This can be done by either console.log() or by using the debugger keyword.

## What is function hoisting?

Hoisting is a mechanism in JavaScript where the function declarations and variables are moved to the scope’s top before the code is executed. This means that no matter where the variables and functions are declared, regardless whether the scope is local or global, the functions are moved to the top of the scope.

**Note:-** **[Ionic Testing](https://www.lambdatest.com/testing-cloud/intel-xdk-testing?utm_source=devto&utm_medium=organic&utm_campaign=apr25_kj&utm_term=kj&utm_content=testing_cloud)** - A scalable and reliable online testing cloud for both manual and automation testing of Ionic websites

## Naming Conventions for Variables in JavaScript

While naming the variables, we have to follow certain rules –

* Do not use keywords that are reserved by JavaScript. For example — Boolean, break etc.

* Don’t start the variable name with a number. Start it with a ‘_’ or an alphabet. For example, instead of 123func, write func123 or _123func.

* Variables are case sensitive. ‘Func’ and ‘func’ will be treated differently.

Apart from the javascript interview questions that I mentioned above, there could be many common JS questions which are asked during interviews. It all depends on the mindset of the interviewer and the section in which he is strong. Sharpen your skills in all the basics and you will be ready to crack the next interview. I hope this blog would be useful to those of you who are preparing for Javascript interview. Also, feel free to share those Javascript interview questions that left a lasting impression in your mind.