JavaScript Interview Questions and Answers
JavaScript Interview Questions and answers for experienced
Set 1
1. What is JavaScript
JavaScript is a lightweight client and server web-based scripting object-oriented programming language contract know as JS. It is High-level, Just in time compiled, Interpreted, and supports multi-paradigm language.
It makes us implement dynamic features on the like content update, interactive map, animation 2D/3D.JavaScript code inserted into HTML and executed by the Web browsers. It follows the ECMAScript Standard.
2. What are datatypes in JavaScript
- Primitive Data Types
- undefined
- Boolean
- Number
- String
- BigInt
- Symbol
- Non-primitive Data Types
- Object
- Set
- Map
- Date
3. What are differences in JavaScript and JScript?
Jscript
- JScript is a scripting programming language that is similar to JavaScript.It is developed by Microsoft.
- Microsoft reverse-engineering JavaScript and develop its own language name it Jscript to avoid trademark with Oracle.
- It uses only in Microsoft browser Internet Explorer.
- It supports active content creation.
- It can access browser objects.
JavaScript
- JavaScript Developed by Oracle.
- It supports almost all browsers.
- Javascript does not support active content creation
- It does not access browser object
4.What is the difference between undefined and null in JavaScript?
Undefined: When we declare any variable with Var and does not assign value to it the default it is undefined.
var s;
console.log(s); //undefined
Null: whereas the null is the value we assign it to variable manually.
var s = null;
console.log(s); //null
5. is JavaScript a case-sensitive language? Explain with example
Yes, JavaScript is case-sensitive, In this example small ‘s’,capital ‘S’ are considered as a different variable
var s = 10;
var S=12
console.log(`small s value = ${s},capital S value = ${S}`);
Output
small s value = 10,capital S = 12
6.What is the difference between =, “==” and “===” in JavaScript.
= : This assignment operator use to assign value to a variable.
==: This comparison operator used to compare the value of the variable does not compare the datatype.
=== : This comparison operator used to compare the value of two variable as well as datatype of variable.
const a = 10;
let b = "10";
console.log(a==b) // return true value are same
console.log(a===b)// false because value is same datatype are not same
7. What is an object in JavaScript ?Explain with example.
The object is an unorder-collection of related data, contain property in form of key and values pair. The keys in an object can be variable or any method. We create an object with curly brackets{}. The key to an object is “String” and “Value” can be anything.
var grocerries = {
name: "Apple",
price: 23,
name:"Sephati",
price:45
};
console.log(grocerries)
//Output
{ name: 'Sephati', price: 45 }
8. What are the loops available in JavaScript?
- While loop
- For loop
- For-in loop
- For-each loop
- for of loop
Explanation of All Loop with examples
9. What is the typeof operator in JavaScript how it works give example?
The typeof operator makes us get the type of a variable(operand) and return data type as a string. A variable(operand) can be a function, an object.
There are two way to use it
typeof operand
typeof(operand)
console.log(typeof 40); //number
console.log(typeof 35 ); //number
let a = 'once'
console.log(typeof(a)); //string
10. What are the falsy values in JavaScript?
A falsy value is that evaluate to False When checking variable value. These are the Falsy Value in Javascript.
const Falsy_val = ['', 0, null, undefined, NaN, false];
11.Difference between undefined and undeclared
Undefined: When we declare a variable with Var and does not assign value to it. Its default value is undefined.
var animal;
console.log(animal)
//output: undefined
Undeclared: We did not declare the variable and try to access the variable it is called an undeclared variable. Accessing undeclare variable throw reference error.
//We are printing undeclare variable num.
console.log(num)
//Output:ReferenceError: num is not defined
12.How to find operating System in Javascript
Navigator.appversion // use to find the operating system
13.How to declare an array and append value in JavaScript?
var a = [];
a[a.length]='First element'
console.log(a)
//Output
[ 'First element' ]
14. What is use of break & continue in JavaScript?
- break: The break statement use to come out of the loop or any code.
- continue: the continue statement use to continue running the loop.
15. Difference between window and document in JavaScript.
Window: A window is a global object that represents the browsers window, loaded firstly in the browser. It has properties length, inner-width, inner-height, Window.location, window. history, window.screen, window.status, window.document.
Document: is document object model/DOM, represent the Web page in the browser like x.asp, x.php,x.html, etc that converted into DOM when load in the browser.A document load in the window Object know as the window Object property. Document object has these property document.activeElement,document.body,document.cookie,document.characterSet
16. What is strict mode in JavaScript?
“use strict” is introduce in ES5 it allows us to use strict mode on code, We can apply it to the entire script or function, module. It helps us avoid some silent JavaScript error.It use to fix that error that are difficult for developer to optimized.
strict mode on declare and accessing variable.
function returnxyz(){
"use strict";
var1 = 123;
return var1;
}
We are accessing the non-declared variable, this program will not throw an error. because we are using strict mode.
17.What is instanceof operator in JavaScript?
The instanceof operator make us check the type of any object at run-time. if object is instance of a particular type then return true else false.
function month(monthname,days) {
this.name = monthname;
this.days = days;
}
const newmonth = new month('jan', 31);
console.log(newmonth instanceof month);
//Output:true
18.What will be the output of this program?
var str = new String("newstring");
str instanceof String;
//Output :true.
19.Explain negative infinity.
Number.NEGATIVE_INFINITY property represents the negative Infinity value.It is static property of JavaScript Number.We use it Number.NEGATIVE_INFINITY.
var Number = (-Number.MAX_VALUE) * 2
console.log(Number)
Output
-Infinity
20. How to handle exceptions in JavaScript?
To handle exception in JavaScript we use try/catch block .
21.What is a function(named function)? How to define one in JavaScript.
The function is the block of code inside in function body (curly brackets {}). that is used to perform some action. We give a name to function as soon as declare that why it calls named function.
function funct_name()
{
//funtion code here
}
//declare of function
function add()
{
var a = 37,b=10;
console.log(a+b);
}
//calling the function
add();
22.What is an anonymous function and how to define in JavaScript?
A function without a name is called an anonymous function. Here is the function without a name and displaying a message.
let print = function () {
console.log('Anonymous function');
};
print();
//Output: Anonymous function
23. Can we pass the anonymous function to another function as an argument
Yes we can pass the anonymous function to another function as an argument
setTimeout(function () {
console.log('code run after 1 second')
}, 1000);
//Output:code run after 1 second
24. What is arrow function in JavaScript?why we use it?
Arrow function introduced in ES6(ECMA 2015), it provides a shorthand Syntax to declare an anonymous function
let print = () => console.log('Anonymous function');
print();
//Output:Anonymous function
25. How we can add external JavaScript File.
<script type=”text/javascript” src=” external.js”></script>
26. What is NaN in JavaScript
The Nan property represents “Not a number” and makes us find a value that is not a legal number.
We find the value of a number is NaN, isNaN() function available is Javascript
console.log(isNaN("Greet")) //true
console.log(isNaN(45)) // false
console.log(isNaN(true))//true
console.log(isNaN(false)) // false
console.log(isNaN(undefined)) // true
27. What is a callback function JavaScript?Explain with an example?
A callback function is a function pass into another function as argument javascript, which is used later to complete in outer function to complete some task.
Synchronous: The callback function execute immediate.
function alarm(time) {
alert('it is ' + time);
}
function Alarmuse(callback) {
let time =' 5 time To wake up '
callback(time);
}
Alarmuse(alarm);
//Output :it is 5 time To wake up
Asynchronous: This callback function is executed after the asynchronous action completely. Calling a function using ‘then‘ after promise fulfill or reject is an example of Asynchronous call back function
const promise1 = new Promise((resolve, reject) => {
resolve('Success!');
});
promise1.then((value) => {
console.log(value);
});
//Output: Success!
28.What are the way to declare variables in JavaScript.
- Var: The variable declared with var has global scope.
- Let: variables declared with let have block scope and we can redeclare in sub-block and reassign them
- const: Variables declare with const have block scope, We cant reassign and modified their value.
29.What is variable hoisting in JavaScript?Explain with an example.
When the JavaScript engine executes any of the code, It creates a global execution plan that includes creation and execution.
While Creation Phase it moves variable and function declaration to the top of their code.
Variable Hoisting
It means we use a variable before it is declared or assign value. In the case of ‘Var‘ code run fine no error.
Example
//Using before declarttion
console.log(s);//ReferenceError: Cannot access 's' before initialization
console.log(t); //ReferenceError: Cannot access 'm' before initialization
console.log(m);
var s=10;
let t=10;
const m = 12;
Let and Const throw error o if used before declaration.
Function hoisting
When we call function before their declaration is called function hoiting.
let a = 20, b = 10;
let mul_res = mul(a,b);
console.log(mul_res);
function mul(a, b){
return a * b;
}
Output = 300
30.Which loop in JavaScript calls the callback function on each element?
for each loop call the callback function on each element in JavaScript
let fruits = [
"apple", "orange", "Banana", "KIWI","Cherry"
]
fruits.forEach(function (fruit, index) {
console.log(`${fruit} ${index}`);
});
Output
apple 0
orange 1
Banana 2
KIWI 3
Cherry 4
Set 2: JavaScript Interview Questions and Answers