JavaScript Interview Questions: Basic JavaScript Interview Questions. JavaScript is a lightweight, interpreted programming language.
Basic JavaScript Interview Questions
Q:- What is JavaScript?
JavaScript(JS) is an object-based, lightweight and cross-platform, high-level, interpreted scripting language.
Q:- Is JavaScript case sensitive language?
Yes, JavaScript is a case-sensitive scripting language. variable's name, keywords, methods, event handlers are all case sensitive.
Q:- What data types are supported in Javascript?

JavaScript provides different data types to hold different types of values.

There are mainly category of two types of data types available in JavaScript.

  1. Primitive data type
  2. Non-primitive (reference) data type
Primitive Data Types:
  • Boolean
  • Number
  • String
  • Null
  • Undefined
  • Symbol
Non-Primitive Data Types:
  • Object
  • Array
Q:- What are the pop up boxes available in JavaScript?
  1. Alert Box
  2. Confirm Box
  3. Prompt Box
alert("JavaScript Interview Questions"); // Alert Message confirm("Are you sure?"); // Return True Or False prompt("Please enter your name"); // Return user provided input
You may also like - Advanced JavaScript Interview Questions
Q:- What is the DOM and BOM in JavaScript?
Document Object Model (DOM):

DOM stands for Document Object Model, DOM: This consists of objects that have to deal with the currently loaded page, which is also called the document.

#Example: document

Browser Object Model (BOM):

BOM stands for Browser Object Model. Unlike DOM, there is no standard defined for BOM, hence different browsers implement it in different ways. Typically, the collection of browser objects is collectively known as the Browser Object Model.

BOM is superset of DOM.

#Example: navigator, screen, location, frames,history, XMLHttpRequest etc

Q:- What is the Window in JavaScript?
Window is root global object in JavaScript.

All global JavaScript objects, functions, and variables automatically become members of the window object.

Even the document object (of the HTML DOM) is a property of the window object.

window.document.getElementById("id"); is the same as document.getElementById("id");
Q:- What is the difference between defer and async?
defer scripts are executed once the parser has finished it's job. defer scripts also gaurantees the order of execution in which they were added in page.

defer will download the file/script during HTML parsing, execute file/script only when the parser has finished it's job.

async scripts are executed as soon as the script is loaded. async scripts doesn't gaurantees the order of execution in which they were added in page.

async will download the file/script during HTML parsing, and halt the parser and execute the file/script and then again parser start it's job.

Q:- What is Strict mode in JavaScript?
"use strict"; tells browser that JavaScript code should be executed in "strict mode".

Strict mode is a new feature of ECMAScript 5 or ES5.

Benifits of using "use strict"
  1. It makes code debugging more easier.
  2. It eliminates javascript silent errors and throw errors.
  3. use strict enforces strict parsing and error handling.
  4. Strict mode makes it easier to write "secure" JavaScript.
  5. It is a good practice, developer should use - "use strict" mode.
  6. It is supported by all modern browsers.
"use strict"; PI = 3.14; //Throw and Error because PI is not declared
"use strict"; const PI = 3.14; //Output: 3.14
You may also like - Node Js Interview Questions And Answers
Q:- How to write comments in JavaScript?

There are two types of comments in JavaScript.

  1. Single Line Comment: It is represented by double forward slash.
  2. Multi Line Comment: It is represented by forward slash with asterisk symbol.
//var a = 10; //var b = 20; //var c = a * b; //Multiplication of a and b is store in c variable //document.write("Multiplication of " + a + " and " + b + " is " + c); function SingleLineComment() { document.write("Example of Single-line Comments in JavaScript!!"); } SingleLineComment();
/* var a = 10; var b = 20; var c = a * b; //Multiplication of a and b is store in c variable document.write("Multiplication of " + a + " and " + b + " is " + c); */ function MultilineComment() { document.write("Example of Multi-line Comments in JavaScript!!"); } MultilineComment();
Q:- What is the difference between == and ===?

The == operator checks values only whereas === checks values and data type.

Q:- window.onload vs. $(document).ready() method?

The $(document).ready() event occurs as early as the HTML document start loading, while the window.onload event occurs later when all content (e.g. images etc.) has been loaded.

$( document ).ready(function() { // Executes when the HTML document is loaded and the DOM is ready alert("Document is ready"); }); Or the shorthand version: $(function() { // Executes when the HTML document is loaded and the DOM is ready alert("Document is ready"); }); // .load() method deprecated from jQuery 1.8 onward $(window).on("load", function() { // Executes when complete page is fully loaded, including // all frames, objects and images alert("Window is loaded"); });
Q:- How to handle exceptions in JavaScript?

try lets you test a block of code for errors.

catch lets you handle the error.

throw lets you create custom errors.

finally lets you execute code, after try and catch, regardless of the result.

try { eval('alert("Hello world)'); if(x == "") throw "is empty"; } catch(error) { console.error(error); // expected output: SyntaxError: unterminated string literal // Note - error messages will vary depending on browser } finally { //Block of code to be executed regardless of the try/catch result }
Q:- List some important HTML DOM Mouse Events?

HTML DOM mouse events

  • onclick
  • ondblclick
  • mousemove
  • mousedown
  • mouseover
  • mouseout
  • mouseup
You may also like - Javascript ES6 Interview Questions
Q:- 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

var myString="FullStackTutorials"; console.log(myString.length-1); //Output: 17
Q:- 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.

var myVar= "FullStackTutorials!" console.log(myVar.valueOf()); //Output: FullStackTutorials!
Q:- How can you create an array in Javascript?

There are 3 different ways to create an array in Javascript.

  1. By Array Literal

    Example: var myArray = [value1,value2...valueN];

  2. By creating Instance of Array

    Example: var myArray = new Array();

  3. By using an Array constructor

    Example: var myArray = new Array('value1','value2',...,'valueN');

Q:- How to add an element [Begnning, End, At Specific Position] to an array in JavaScript?

You can use unshift and push function

var myArr = ['Apple', 'Orange', 'Mango']; myArr.unshift('Pineapple'); // At the Begnning myArr.push('Strawberries'); // At the end console.log(myArr); //  ["Pineapple", "Apple", "Orange", "Mango", "Strawberries"]

You can also use ES6 spread operator

var myArr = ['Apple', 'Orange', 'Mango']; myArr = ['Pineapple', ...myArr, 'Strawberries']; console.log(myArr); //  ["Pineapple", "Apple", "Orange", "Mango", "Strawberries"]

You can use splice function add an element into an array at any specific postion.

var myArr = ['Apple', 'Orange', 'Mango']; myArr.splice(0, 0, "Pineapple"); // At 0th Index myArr.splice(4, 0, "Strawberries"); // At 4rth Index console.log(myArr); //  ["Pineapple", "Apple", "Orange", "Mango", "Strawberries"]
Q:- Difference between the substr() and substring() functions in JavaScript?

The substr() function has the form substr(startIndex,length). It returns the substring from startIndex and returns length number of characters.

var s = "FullStackTutorials"; console.log(s.substr(1,4)); //Output: ullS

The substring() function has the form substring(startIndex,endIndex). It returns the substring from startIndex up to endIndex – 1.

var s = "FullStackTutorials"; console.log(s.substring(1,4)); //Output: ull
Q:- Explain Frames in JavaScript?

Frames allow you to divide the page into several rectangular areas and to display a separate document in each rectangle. Each of those rectangles are called a "frame".

Q:- What are the common Errors in JavaScript?

In JavaScript, there are the following three types of errors:

  1. Syntax Error
  2. Runtime Error
  3. Logic Error
You may also like - React Js Interview Questions and Answers
Q:- Explain Page Re-direction in JavaScript?

Page redirection means moving to another location or the page, using JavaScript at the client side.

Using window.location

window.location = "";

Using window.location.href

window.location.href = "";

Using window.location.assign

window.location.assign = "";

Using window.location.replace

window.location.replace = "";

using"", "OpenWindow", "status = 1, height = 250, width = 250, resizable = 0")
Q:- What will be the output of 10+20+"30" explain with reason?

Ans: - 3030

Reason: 10 and 20 are integer, So they will be added numerically. And since 30 is a string, So it will be concat and final output will be 3030.

Q:- What will be the output of "30"+20+10 explain with reason?

Ans: - 302010

Reason: Since 30 is a string which is at the starting of expression, So it will be concat with all later values so final output will be 302010.

Q:- Write the output of the following javascript programs?
console.log(typeof undefined == typeof NULL)

true, becasue javascript is a case-sensitive, so typeof NULL is undefined

console.log(typeof undefined == typeof null)

false, becasue typeof null is object