List Headline Image
Updated by edureka.co on Oct 18, 2021
 REPORT
edureka.co edureka.co
Owner
15 items   2 followers   1 votes   23 views

Top JavaScript Interview Questions You Must Prepare in 2019

If you are planning to start your career in JavaScript and you wish to know the skills related to it, now is the right time to dive in, when the technology is in its blossoming state. JavaScript Interview Questions will provide you with in-depth knowledge and help you prepare for your interviews.

1

What is JavaScript?

JavaScript is a lightweight, interpreted programming language with object-oriented capabilities that allows you to build interactivity into otherwise static HTML pages. The general-purpose core of the language has been embedded in Netscape, Internet Explorer, and other web browsers.

2

What are the data types supported by JavaScript?

The data types supported by JavaScript are:

Undefined
Null
Boolean
String
Symbol
Number
Object

3

How can you create an object in JavaScript?

JavaScript supports Object concept very well. You can create an object using the object literal as follows −

var emp = {
name: "Daniel",
age: 23
};

4

How can you create an Array in JavaScript?

You can define arrays using the array literal as follows-

var x = [];
var y = [1, 2, 3, 4, 5];

5

What are the variable naming conventions in JavaScript?

The following rules are to be followed while naming variables in JavaScript:

You should not use any of the JavaScript reserved keyword as variable name. 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, 123name is an invalid variable name but _123name or name123 is a valid one.
JavaScript variable names are case sensitive. For example, Test and test are two different variables.

6

How to create a cookie using JavaScript?

The simplest way to create a cookie is to assign a string value to the document.cookie object, which looks like this-

Syntax :

document.cookie = "key1 = value1; key2 = value2; expires = date";

7

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:
(i) getElementById(‘idname’): Gets an element by its ID name
(ii) getElementsByClass(‘classname’): Gets all the elements that have the given classname.
(iii) getElementsByTagName(‘tagname’): Gets all the elements that have the given tag name.
(iv) querySelector(): This function takes css style selector and returns the first selected element.

8

What is an event bubbling in JavaScript?

Event bubbling is a way of event propagation in the HTML DOM API, when an event occurs in an element inside another element, and both elements have registered a handle for that event. With bubbling, the event is first captured and handled by the innermost element and then propagated to outer elements. The execution starts from that event and goes to its parent element. Then the execution passes to its parent element and so on till the body element.

9

How can you convert the string of any base to integer in JavaScript?

The parseInt() function is used to convert numbers between different bases. It takes the string to be converted as its first parameter, and the second parameter is the base of the given string.

For example-

parseInt("4F", 16)

10

What are Exports & Imports?

Imports and exports help us to write modular JavaScript code. Using Imports and exports we can split our code into multiple files. For example-

//------ lib.js ------
export const sqrt = Math.sqrt;
export function square(x) {
return x * x;
}
export function diag(x, y) {
return sqrt(square(x) + square(y));
}

//------ main.js ------
{ square, diag } from 'lib';
console.log(square(5)); // 25
console.log(diag(4, 3)); // 5

11

What is the difference between Call & Apply?

The call() method calls a function with a given this value and arguments provided individually.

Syntax-

fun.call(thisArg[, arg1[, arg2[, ...]]])
The apply() method calls a function with a given this value, and arguments provided as an array.

Syntax-

fun.apply(thisArg, [argsArray])

12

How to empty an Array in JavaScript?

There are a number of methods you can use to empty an array:

Method 1 –

arrayList = []
Above code will set the variable arrayList to a new empty array. This is recommended if you don’t have references to the original array arrayList anywhere else, because it will actually create a new, empty array. You should be careful with this method of emptying the array, because if you have referenced this array from another variable, then the original reference array will remain unchanged.

Method 2 –

arrayList.length = 0;
The code above will clear the existing array by setting its length to 0. This way of emptying the array also updates all the reference variables that point to the original array. Therefore, this method is useful when you want to update all reference variables pointing to arrayList.

Method 3 –

arrayList.splice(0, arrayList.length);
The implementation above will also work perfectly. This way of emptying the array will also update all the references to the original array.

Method 4 –

while(arrayList.length)
{
arrayList.pop();
}
The implementation above can also empty arrays, but it is usually not recommended to use this method often.

13

What is the reason for wrapping the entire content of a JavaScript source file in a function book?

This is an increasingly common practice, employed by many popular JavaScript libraries. This technique creates a closure around the entire contents of the file which, perhaps most importantly, creates a private namespace and thereby helps avoid potential name clashes between different JavaScript modules and libraries.
Another feature of this technique is to allow for an easy alias for a global variable. This is often used in jQuery plugins.

14

What are escape characters in JavaScript?

JavaScript escape characters enable you to write special characters without breaking your application. Escape characters (Backslash) is used when working with special characters like single quotes, double quotes, apostrophes and ampersands. Place backslash before the characters to make it display.

For example-

document.write "I am a "good" boy"
document.write "I am a "good" boy"

15

What is the ‘Strict’ mode in JavaScript and how can it be enabled?

Strict mode is a way to introduce better error-checking into your code.

  • When you use strict mode, you cannot use implicitly declared variables, or assign a value to a read-only property, or add a property to an object that is not extensible.
  • You can enable strict mode by adding “use strict” at the beginning of a file, a program, or a function.

To keep reading more questions, click here.