Important Javascript Questions and Answer
Discover the most Important JavaScript Questions and Answers. Enhance your coding skills and interview readiness with this essential guide to JS fundamentals.
1. What is JavaScript? What is the role of JavaScript engine?
JavaScript is a programming language that is used for converting static web pages to
interactive and dynamic web pages.
A JavaScript engine is a program present in web browsers that executes JavaScript code.
JavaScript Engines
V8 (Chrome)
Spider-Monkey (Firefox)
Javascript-Core (Safari)
Chakra (Edge)
2. What are Client side and Server side?
A client is a device, application, or software component that requestsand consumes services
or resources from a server.
A server is a device, computer, or software application that provides services, resources, or
functions to clients.
3. What are variables? What is the difference between var, let, and const?
Using the flexible and popular programming language JavaScript, programmers may create
dynamic and interactive online apps and web pages. The ability to store and change data by
using variables is one of the fundamental ideas in JavaScript. Var, let, and const are the three
main ways to declare variables in JavaScript or we can say that these are the key words. Each
of these keywords has a certain purpose and has a different scope. The differences between
var, let, and const in JavaScript with examples are :-
Var:
From the starting of JavaScript, var has been the most traditional method of declaring
variables. Var-declared variables are function-scoped or we can say that var is function
scope, which means their scope is constrained to the function in which they are specified. It
will still be possible to access a variable that is declared using the var keyword inside of a
block, such as a loop or conditional expression.
function mine() {
if (true) {
var x = 10;
}
console.log(x); // Outputs 10
}
mine();
In this example, x is declared using var inside the if block, but it is still accessible outside the
block.
let:
let which was added in ECMAScript 6 (ES6), it follow block-scoped method of declaring
variables. Because of this, let is especially helpful for prevent variable hoisting and in scope-
related errors.
function mine() {
if (true) {
let y = 20;
}
console.log(y);
}
mine();
In this example, y is declared using let inside the if block, and it is not accessible outside the
block.
const:
Constants (const), when we use const keyword with variables then their initial value cannot
be changed. Const keyword also come in ES6. const is thus a good choice for declaring
variables that shouldn’t change while a block of code is being executed.
function mine() {
const z = 30;
z = 40;
}
mine();
In this example, z is declared using const and is immediately assigned the value 30. When
we attempt to change its value to 40, a TypeError is thrown because const variables cannot
be reassigned.
For Free Demo classes Call: 020-71173125
Registration Link: Click Here!
4. What are some important string operations in JS?
Concatenation with concat()
Slicing with slice()
Substring Extraction with substring()
Substring Extraction with substr()
replace()
search()
toLocalLowerCase()
toLocalUpperCase()
indexOf()
includes()
lastIndexOf()
charCodeAt()
trim()
charAt()
valurOf()
split()
toString()
match()
//add multiple string
let str1 = "This"
let str2 = "Good"
let result = str1 + " " + str2
console.log(result);
//output: This Good
//using concat methods
let str1 = "This"
let str2 = "Good"
let result = str1.concat(" " , str2)
console.log(result);
//output: This Good
//using substring methods
let str1 = "This"
let str2 = "Good"
let result = str1 + " " + str2
let substring = result.substring(5,11)
console.log(substring);
//output: Good
//using length methods
let str1 = "This"
let str2 = "Good"
let result = str1 + " " + str2
let substring = result.substring(5,11)
console.log(substring.length);
//output: 4
//using toLocaleUpperCase methods
let str1 = "This"
let str2 = "Good"
let result = str1 + " " + str2
console.log(result.toLocaleUpperCase());
//output: THIS GOOD
//using toLocaleLowerCase methods
let str1 = "This"
let str2 = "Good"
let result = str1 + " " + str2
console.log(result.toLocaleLowerCase());
//output: this good
//using split methods
let str1 = "This"
let str2 = "Good"
let result = str1 + " " + str2
let arr = result.split(" ")
console.log(arr);
//output: [ 'This', 'Good' ]
//using replace methods
let str1 = "This"
let str2 = "Good"
let result = str1 + " " + str2
console.log(result.replace("This", "Mine"));
//output: Mine Good
//using replace methods
let str1 = " This Good "
console.log(str1);
console.log(str1.length);
let trim = str1.trim();
console.log(trim);
console.log(trim.length);
//output: This Good
//15
//This Good
//9
5. What is DOM? What is the difference between HTML and DOM?
The DOM(Document Object Model) represents the web page as a tree-like structure that allows JavaScript to dynamically access and manipulate the content and structure of a web
page.
In other words, a website made up of an HTML and CSS document. The browser creates a
representation of that document known as Document Object Model (DOM). This document
enables Javascript to access and manipulate the elements and styles of a website and also add
functionality to our page. So by this we can say that DOM have power to change our
website.
So HTML is the document and DOM have power to manipulate this document by using JS.
6. What is array? Explain different methods present in arrays in javascript?
Array is a collection of elements of the same datatype. Basically, array is
1.push() => adding element at the end of the array
//0 1 2 3 4 5
let x=[4,”hello”,27.2,true,82,2];
x.push(“welcome”);
document.write(“<br>after adding element at the end of the array : “+x);
Output:
[4,”hello”,27.2,true,82,2,”welcome”];
2. unshift()=> adding element at first position of the array
//0 1 2 3 4 5
let x=[4,”hello”,27.2,true,82, 2];
x.unshift(100); //shift()
document.write(“<br>after adding element at the start array : “+x);
Output:
[100,4,”hello”,27.2,true,82,2];
3. Splice => adding element at middle position of the array
//0 1 2 3 4 5
let x=[4,”hello”,27.2,true,82, 2];
x.splice( 2 ,0,”js”); //here,2 is the index postion on whcih we want add an element “js”
//the second argument is 0 which indicates it will not delete the element which is already positioned on index 2
//x.splice( 2 ,0,”js”,”session”);
document.write(“<br>after adding element at the middle of the array : “+x);
4. concat()
let x=[3,6,7,3,7,9];
let y=[“raj”,”riya”,”jay”,true];
document.write(“<br>after x concat y = “+x.concat(y));
Output:
after x concat y = 3,6,7,3,7,9,7,8,99,hello,78.5
5.filter()=> filter function is used to filter an array based on given predicate/condition
//WAP to findout the elements of an array having length equals to 3
let data=[“ram”,”hello”,”welcome”,”jay”,”run”,”program”];
document.write(“original array = “+data);
//In below code, (p) => p.length == 3 ) is called as “arrow function”
//filter function is used to filter an array based on given predicate/condition
//here,in below case filter function takes one one value at a time from array “data” and //store it into variable “p”
document.write(“<br>after fiter data = “+data.filter((p) => p.length == 3 ));
6. indexOf and lastIndexOf => is used to findout the index of first and last occurrence of given element respectively
//0 1 2 3 4 5 6 7 8 9
let x=[3,23,”hello”,7,3,”hello”,9,12,”hello”,99];
document.write(“<br>array x = “+x);
//indexOf()=> method findout FIRST occurrence of given element from the array
document.write(“<br>first index of hello is = “+x.indexOf(“hello”));
//lastIndexOf()=> method findout LAST occurrence of given element from the array
document.write(“<br>last index of hello is = “+x.lastIndexOf(“hello”));
//indexOf()=> method findout MIDDLE occurrence of given element from the array
document.write(“<br>middle index of hello is = “+x.indexOf(“hello”,3)); //findingout indexof hello after 3rd position
Output:
array x = 3,23,hello,7,3,hello,9,12,hello,99
first index of hello is = 2
last index of hello is = 8
middle index of hello is = 5
Do watch the video on Java: Click Here
Author:-
Pooja Nandode-Bhavsar
Call the Trainer and Book your free demo class for JavaScript now!!!
© Copyright 2020 | SevenMentor Pvt Ltd.