surbhidighe / Javascript-Output-Based-Questions

Explore the world of JavaScript through practical output-based questions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JavaScript output based interview questions


Click ⭐ if you like it!!

Every contribution counts, regardless of its size. I value and appreciate the efforts of all contributors, from beginners to seasoned developers. Join me on this exciting journey of open-source collaboration. Together, let's build something amazing! 🀝


Contribution Guidelines

  • πŸ‘‰ Please ensure that your contributions adhere to the coding style and guidelines of this project
  • πŸ‘‰ Include clear and concise commit messages for all your commits
  • πŸ‘‰ Provide a detailed description of your changes in the pull request.
  • πŸ‘‰ Be respectful and considerate towards other contributors.

1. What will be the output

let arr = [1, 2, 3, 4, 5, -6, 7];
arr.length = 0;
console.log(arr);
View Answer
  • Output : [ ]
  • Reason : The length of the array has been set to 0, so the array becomes empty.

πŸ” Scroll to Top

2. What will be the output

x = 10;
console.log(x);
var x;
View Answer
  • Output : 10
  • Reason : The declaration of the variable x is hoisted to the top of its scope.

πŸ” Scroll to Top

3. What will be the output

let a = { x: 1, y: 2 }
let b = a;
b.x = 3;
console.log(a);
console.log(b);
View Answer
  • Output : { x: 3, y: 2 } { x: 3, y: 2 }
  • Reason : 'a' and 'b' both are pointing to the same reference.

πŸ” Scroll to Top

4. What will be the output

for(var i = 0; i < 10; i++){
    setTimeout(function(){
      console.log("value is " + i);
  })
}
View Answer
  • Output : 10 times, "value is 10"
  • Reason : "var" has a function scope, and there will be only one shared binding for the iterations. By the time the setTimeout function gets executed, the for loop has already completed and the value of the variable i is 10.

πŸ” Scroll to Top

5. What will be the output

for(let i = 0; i < 10; i++){
    setTimeout(function(){
      console.log("value is " + i);
  })
}
View Answer
  • Output : 10 times "value is" followed by the value of i in each iteration, from 0 to 9
  • Reason : "let" has a block scope, and a new binding will be created for each iteration. Here, a new variable i is created and has a different value for each iteration of the loop.

πŸ” Scroll to Top

6. What will be the output

function hello() {
  console.log("1");
    setTimeout(() => {
        console.log("2");
    })
  console.log("3");
}
hello();
View Answer
  • Output : "1" followed by "3", and then after a small delay, "2"
  • Reason : console.log("1") statement logs "1" to the console. Then setTimeout() function is set to execute the callback function in the next event loop iteration and logs "3" to the console.

πŸ” Scroll to Top

7. What will be the output

let f = "8";
let a = 1;
console.log((+f)+a+1);
View Answer
  • Output : 10
  • Reason : The expression (+f) is a shorthand way to convert the string value of f to a number. Therefore, (+f) evaluates to 8.

πŸ” Scroll to Top

8. What will be the output

let a = 10;
if(true){
   let a = 20;
   console.log(a, "inside");
}
console.log(a, "outside");
View Answer
  • Output : 20, "inside" and 10, "outside"
  • Reason : The variable "a" declared inside "if" has block scope and does not affect the value of the outer "a" variable.

πŸ” Scroll to Top

9. What will be the output

var a = "xyz";
var a = "pqr";
console.log(a)
View Answer
  • Output : "pqr"
  • Reason : Both the variables are declared using "var" keyword with the same name "a". The second variable declaration will override the first variable declaration.

πŸ” Scroll to Top

10. What will be the output

const arr1 = [1, 2, 3, 4];
const arr2 = [6, 7, 5];
const result = [...arr1, ...arr2];
console.log(result);
View Answer
  • Output : [1, 2, 3, 4, 6, 7, 5]
  • Reason : Spread operator (...) concatenates the two arrays into "result" array.

πŸ” Scroll to Top

11. What will be the output

const person1 = { name: 'xyz', age: 21 };
const person2 = { city: 'abc', ...person1 };
console.log(person2);
View Answer
  • Output : { city: 'abc', name: 'xyz', age: 21 }
  • Reason : Spread operator (...) copies all the properties from person1 into person2.

πŸ” Scroll to Top

12. What will be the output

console.log(5 < 6 < 7);
View Answer
  • Output : true
  • Reason : In JavaScript, the < operator evaluates expressions from left to right. First, the expression 5 < 6 is evaluated, resulting in true because 5 is less than 6. Then, the expression true < 7 is evaluated. In this case, JavaScript performs type coercion and converts true to the number 1. Therefore, the expression becomes 1 < 7, which is true.

πŸ” Scroll to Top

13. What will be the output

console.log(7 > 6 > 5);
View Answer
  • Output : false
  • Reason : In JavaScript, the > operator evaluates expressions from left to right. First, the expression 7 > 6 is evaluated, resulting in true because 7 is greater than 6. Then, the expression true > 5 is evaluated. In this case, JavaScript performs type coercion and converts true to the number 1. Therefore, the expression becomes 1 > 5, which is false.

πŸ” Scroll to Top

14. What will be the output

console.log(0 == false);
console.log(1 == true);
View Answer
  • Output : true, true
  • Reason : The == operator converts operands to a common type before making the comparison. In both the cases, the boolean value will be converted to a number, i.e., false is converted to 0 and true is converted to 1. So, the expression 0 == false is equivalent to 0 == 0 and 1 == true is equivalent to 1 == 1.

πŸ” Scroll to Top

15. What will be the output

console.log([11, 2, 31] + [4, 5, 6]);
View Answer
  • Output : "11,2,314,5,6"
  • Reason : The + operator is used for both addition and string concatenation. When you try to concatenate two arrays using the + operator, the arrays are converted to strings and then concatenated together. In this case, the arrays [11, 2, 31] and [4, 5, 6] are converted to strings as "11,2,31" and "4,5,6" respectively. Then, the two strings are concatenated, resulting in "11,2,314,5,6".

πŸ” Scroll to Top

16. What will be the output

console.log({} == {}); 
console.log({} === {});
View Answer
  • Output : false, false
  • Reason : When you compare objects using == or ===, it checks if they refer to the exact same object. So even if they are looking same, they are pointing to different memory locations.

πŸ” Scroll to Top

17. What will be the output

let x = 5;
let y = x++;
console.log(y);
console.log(x)
View Answer
  • Output : 5, 6
  • Reason : The post-increment operator increments and returns the value before incrementing.

πŸ” Scroll to Top

18. What will be the output

let x = 5;
let y = ++x;
console.log(y);
console.log(x)
View Answer
  • Output : 6, 6
  • Reason : The pre-increment operator increments and returns the value after incrementing.

πŸ” Scroll to Top

19. What will be the output

console.log('apple'.split(''));
View Answer
  • Output : [ 'a', 'p', 'p', 'l', 'e' ]
  • Reason : split method is used to split a string into an array of substrings based on a specified separator.

πŸ” Scroll to Top

20. What will be the output

const arr = [2,3,5,2,8,10,5];
console.log(arr.indexOf(5))
View Answer
  • Output : 2
  • Reason : indexOf method returns the index of the first occurrence of the specified element in the array.

πŸ” Scroll to Top

21. What will be the output

const array = [8, 18, 28, 38];
const result = array.map(element => element + 2)
               .filter((element) => element > 25);
console.log(result);
View Answer
  • Output : [ 30, 40 ]
  • Reason : The code increments each element in the array by 2 using map and filters out elements greater than 25 using filter.

πŸ” Scroll to Top

22. What will be the output

function checkValue(value){
    var result = Array.isArray(value);
    console.log(result);
}
checkValue([1,2,3]);
View Answer
  • Output : true
  • Reason : Array.isArray() method is used to check if the provided value is an array.

πŸ” Scroll to Top

23. What will be the output

function sum(a=5, b=7){
    return a+b;
}
console.log(sum(undefined, 20));
View Answer
  • Output : 25
  • Reason : Here, undefined is passed as the value for parameter a, and 20 is passed for parameter b. When any parameter is undefined, the default value is used.

πŸ” Scroll to Top

24. What will be the output

console.log(10 + "5");
console.log("5" + 10);
View Answer
  • Output : 105, 510
  • Reason : Since one operand is a string, the + operator performs string concatenation.

πŸ” Scroll to Top

25. What will be the output

console.log(10 - "5");
console.log("5" - 10);
View Answer
  • Output : 5, -5
  • Reason : In JavaScript, when the subtraction operator - is used, the operands are converted to numbers before performing the subtraction

πŸ” Scroll to Top

26. What will be the output

console.log(printName());
function printName(){
    return "Hi my name is Bob"
}
View Answer
  • Output : Hi my name is Bob
  • Reason : Regular functions are hoisted to the top. And you can access and call them even before they are declared.

πŸ” Scroll to Top

27. What will be the output

console.log(printName());
const printName = () => {
    return "Hi my name is Bob"
}
View Answer
  • Output : ReferenceError: Cannot access 'printName' before initialization
  • Reason : Arrow functions cannot be accessed before they are initialised.

πŸ” Scroll to Top

28. What will be the output (shallow copy of an object)

const userDetails = {
  firstName: "Surbhi",
  lastName: "Dighe",
  age: 20,
  address: {
    city: "Hyderabad",
    country: "India",
  },
};

let cloneUserDetails = { ...userDetails };
//Updating original object
userDetails.age = 22;
userDetails.address.city = "Banglore";

console.log(cloneUserDetails.age); 
console.log(cloneUserDetails.address.city);
View Answer
  • Output : 20, "Banglore"
  • Reason : cloneUserDetails is created by using the spread syntax ({ ...userDetails }). This syntax creates a shallow copy of the userDetails object, meaning that the top-level properties are copied, but nested objects are still referenced.
  • case 1 : Although userDetails.age was changed to 22, cloneUserDetails still holds the original value of 20. This is because the spread syntax only creates a shallow copy, so the age property of cloneUserDetails remains unchanged.
  • case 2 : The nested address object is still referenced by cloneUserDetails, so when the city property of userDetails.address is changed, it reflects in cloneUserDetails.address as well. Therefore, the output is "Banglore".

πŸ” Scroll to Top

29. What will be the output

function hello(){
console.log(name);
console.log(age);
var name = "Alice";
let age = 21;
}
hello();
View Answer
  • Output : undefined, ReferenceError: can't access lexical declaration 'age' before initialization"
  • Reason for console.log(name) : The variable name (declared with var) is hoisted to the top, so JavaScript knows it exists, but it hasn't been assigned a value yet, so it prints undefined
  • Reason for console.log(age) : The variable age (declared with let) is also hoisted to the top of its scope, but unlike var, it is not initialized until the line where it is declared.

πŸ” Scroll to Top

30. What will be the output

const arr1 = [1,2,3];
const arr2 = [1,2,3];
const str = "1,2,3";

console.log(arr1 == str);
console.log(arr1 == arr2);
View Answer
  • Output : true, false
  • Reason for console.log(arr1 == str) : Javascript compiler performs type conversion. In this case, it converts the array arr1 and the string str to their string representations and then compares them.
  • Reason for console.log(age) : In Javascript arrays are objects and objects are compared by reference. In this case, arr1 and arr2 are pointing to 2 different memory locations

πŸ” Scroll to Top

31. What will be the output

const a = {x : 1};
const b = {x : 1};
console.log(a === b);
console.log(a.x === b.x)
View Answer
  • Output : false, true
  • Reason for console.log(a === b) : This compares whether a and b refer to the exact same object in memory. They are two different objects in memory, so the comparison evaluates to false
  • Reason for console.log(a.x === b.x) : This compares the x property of objects a and b. Since both a.x and b.x have the same value i.e., 1, so the comparison evaluates to true.

πŸ” Scroll to Top