Home >> Interviews Q & A >> JavaScript basic interview questions and answers

JavaScript basic interview questions and answers

JavaScript Interview Questions: Javascript basic interview questions. JavaScript is a lightweight, interpreted programming language.
Q:- What is JavaScript?
JavaScript(JS) is an object-based, lightweight and cross-platform, high-level, interpreted programming languagescripting language.
Q:- Is there any difference between JavaScript and JScript?
No, both are the same. JavaScript was provided by Netscape. Microsoft had just changed its name to JScript.
Q:- Is JavaScript case sensitive language?
Yes, JavaScript is a case-sensitive scripting language. Variable names, 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 two types of data types in JavaScript.
  1. Primitive data type
  2. Non-primitive (reference) data type
Primitive Data Types:
  • Boolean
  • Null
  • Undefined
  • Number
  • String
  • Symbol
Non-Primitive Data Types:
  • Object
  • Array
Q:- What are the pop up boxes available in JavaScript?

Alert -- OK
Confirm -- OK/CANCEL
Prompt -- Input

  1. Alert Box
  2. Confirm Box
  3. Prompt Box
alert("Hello, FullStackTutorials Team");  // Display an 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 'this' keyword in JavaScript?

The this keyword behaves differently in JavaScript compared to other language. In Object Oriented languages, the this keyword refers to the current instance of the class.

In JavaScript the value of this is determined mostly by the invocation context of function ( context.function() ) and where it is called.

In Short - 'this' keyword refers to the object from where it was called.

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.
We manipulate DOM tree with the help of JavaScript and jQuery.

#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.
You can say BOM is super set of DOM.

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

Q:- What is the Window in JavaScript?

There's a root object called window. The window object is supported by all browsers. It represents the browser's window.

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 Strict mode in JavaScript?

Strict Mode is a new feature in ECMAScript 5 or ES5.

The statement "use strict"; instructs the browser to use the Strict mode, which is a reduced and safer feature set of JavaScript.

Benifits of using "use strict"

Strict mode makes several changes to normal JavaScript semantics.

  1. Eliminates some JavaScript silent errors by changing them to throw errors.
  2. Fixes mistakes that make it difficult for JavaScript engines to perform optimizations: strict mode code can sometimes be made to run faster than identical code that's not strict mode.
  3. Strict mode makes it easier to write “secure” JavaScript.
"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 slash with asterisk symbol as /* write comment here */
 //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:- Differences between JavaScript's window.onload and jQuery's $(document).ready() method?

The ready event occurs after the HTML document has been loaded, while the onload event occurs later when all content (e.g. images) also has been loaded.

The onload event is a standard event in the DOM, while the ready event is specific to jQuery. The purpose of the ready event is that it should occur as early as possible after the document has loaded, so that code that adds functionality to the elements in the page doesn't have to wait for all content to load.

$( 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?

The try statement lets you test a block of code for errors.

The catch statement lets you handle the error.

The throw statement lets you create custom errors.

The finally statement 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 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:- 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 = "https://www.fullstacktutorials.com/";   

Using window.location.href

window.location.href = "https://www.fullstacktutorials.com/";   

Using window.location.assign

window.location.assign = "https://www.fullstacktutorials.com/";   

Using window.location.replace

window.location.replace = "https://www.fullstacktutorials.com/";   

using window.open

 window.open("https://www.fullstacktutorials.com/", "OpenWindow", "status = 1, height = 250, width = 250, resizable = 0") 
Q:- What will be the output of 10+20+"30" explain with reason?

Ans: - 102030

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 102030.

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.

Full Stack Tutorials

Author | Blogger @FullStackTutorials | View all articles