JavaScript Coding Questions and Answers | Learn JavaScript Coding Interview Questions Answers to become Full Stack JavaScript Developer | Front-end Developer
JavaScript Coding Questions and Answers

JavaScript Coding Questions and Answers

Q:- What is JavaScript?

JavaScript(JS) is an object-based, lightweight and cross-platform, high-level, interpreted case-sensitive scripting language.

Q:- Method Overloading and Overriding in JavaScript?
  1. Method Overloading - JavaScript does not support Overloading.
  2. Method Overriding - JavaScript supports Overloading.
  3. Suppose, you have defined two or more functions with same function name, then last function will override the previously defined functions.

2.1 Overriding Custom JavaScript Function:
function sum(a, b, c) { return a + b + c; } function sum(a, b) { return a + b; } var result = sum(5, 10, 10); console.log(result);
Output:

15

2.2 Overriding Built-in JavaScript Function:
let alert = msg => { console.log(msg); }; alert("JavaScript - Overriding Built-in JavaScript Function!");
Output:

JavaScript - Overriding Built-in JavaScript Function!

Note: As we all know that by default, alert() function displays the message in the alert box. In the above example we have overriden it.

Q:- What will be the output of following js code snippet?
const number = undefined + 11; if (number === NaN) { document.write("NaN"); } else if (number === 11) { document.write("11"); } else { document.write("other"); }
Output:

other

You may also like - JavaScript Interview Questions
Q:- Reverse a given string using JavaScript?
var str = "Full Stack Tutorials"; var output = str .split("") .reverse() .join(""); document.write(output);
Output:

slairotuT kcatS lluF

Q:- Find the sum of all elements of a given array?
var arr = [1, 2, 5, 10, 20]; //Solution - 1 var sum = 0; for (let i in arr) { sum += arr[i]; } document.write(sum); //Solution - 2 var sum = arr.reduce((a, i) => { return a + i; }); document.write(sum);
Output:

38

Q:- JavaScript startsWith and endsWith?
let name = 'Full Stack Tutorials, Latest Interview Questions and Answers!'; //startsWith console.log(name.startsWith('Full')); // true console.log(name.startsWith('full')); // false console.log(name.startsWith('Tutorials')); // false console.log(name.startsWith('Tutorials', 11)); // true //endsWith console.log(name.endsWith('Answers!')); // true console.log(name.endsWith('answers!')); // false

Remarks:- Both startsWith & endsWith are case-sensitive.

Q:- Find the output?
var a=3; var b=a++; var c=++a; console.log(a,b,c)
Output:

5 3 5

Q:- Consider the two functions below. Will they both return the same thing?
function func1() { return { name: "Full Stack Tutorials" }; } function func2() { return { name: "Full Stack Tutorials" }; } console.log(func1()); console.log(func2());
Output:
//Output: {name: "Full Stack Tutorials"} undefined
Reason:-

The reason for this has to do with the fact that semicolons are technically optional in JavaScript (although omitting them is generally really bad form).

As a result, when the line containing the return statement (with nothing else on the line) is encountered in func2(), a semicolon is automatically inserted immediately after the return statement.

No error is thrown since the remainder of the code is perfectly valid, even though it doesn’t ever get invoked or do anything (it is simply an unused code block that defines a property bar which is equal to the string "Full Stack Tutorials").

This behavior also argues for following the convention of placing an opening curly brace at the end of a line in JavaScript, rather than on the beginning of a new line. As shown here, this becomes more than just a stylistic preference in JavaScript.

Q:- What will be output of following?
console.log(typeof undefined); console.log(typeof null); console.log(typeof NULL); console.log(typeof typeof 1);
Output:
//Output: undefined object undefined string
You may also like - React.js Interview Questions
Q:- How to convert an Object {} into an Array [] in JavaScript?
let obj = { id: "1", name: "Test User", age: "25", profession: "Developer" }; //Method 1: Convert the keys to Array using - Object.keys() console.log(Object.keys(obj)); // ["id", "name", "age", "profession"] // Method 2 Converts the Values to Array using - Object.values() console.log(Object.values(obj)); // ["1", "Test User", "25", "Developer"] // Method 3 Converts both keys and values using - Object.entries() console.log(Object.entries(obj)); //[["id", "1"],["name", "Test User"],["age", "25"],["profession", "Developer"]]
Q:- How to convert an Array [] to Object {} in JavaScript?
let arr = ["1", "Test User", "25", "Developer"]; let arr1 = [["id", "1"],["name", "Test User"],["age", "25"],["profession", "Developer"]]; //Method 1: Using Object.assign() method console.log(Object.assign({}, arr)); //{0: "1", 1: "Test User", 2: "25", 3: "Developer"} // Method 2 Using Spread operator console.log({ ...arr }); //{0: "1", 1: "Test User", 2: "25", 3: "Developer"} // Method 3: Using Object.fromEntries() method console.log(Object.fromEntries(arr1)); //{id: "1", name: "Test User", age: "25", profession: "Developer"}
Questions for you!
Q:- What is the output?
let a = 10; var a = 20; console.log(a);