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
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/numbers of a given array?
There are many ways to solve this problem, we will see following ways.
- using array reduce() method
var arr = [1, 2, 5, 10, 20];
var sum = arr.reduce((a, i) => {
return a + i;
});
document.write(sum);
- using loops (e.g. - for loop)
var arr = [1, 2, 5, 10, 20];
var sum = 0;
for (let i in arr) {
sum += arr[i];
}
document.write(sum);
Output:
38
Q:- Tell the output of following code?
const a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (let i = 0; i < 10; i++) {
setTimeout(() => console.log(a[i]), 1000);
}
for (var i = 0; i < 10; i++) {
setTimeout(() => console.log(a[i]), 1000);
}
Output:
1
2
3
4
5
6
7
8
9
10
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
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:- 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
Note:- 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:- What will be the output of both the fucntions?
function func1() {
return {
name: "Full Stack Tutorials",
};
}
console.log(func1());
Output:
//Output:
{name: "Full Stack Tutorials"}
function func2() {
return;
{
name: "Full Stack Tutorials";
}
}
console.log(func2());
Output:
//Output:
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"}
Q:- Tell the output of following code?
const a = { x: 1, y: 2 };
const b = a;
b.x = 3;
console.log(a);
console.log(b);
Output:
{ x: 3, y: 2 }
{ x: 3, y: 2 }
Reason: both the object are pointing to same reference.
Questions for you!
Q:- What is the output?
let a = 10;
var a = 20;
console.log(a);
Was this interview questions answers helpful?
Share now with your friends!