Skip to content

surbhidighe/Javascript-Output-Based-Questions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 

Repository files navigation

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

Releases

No releases published

Packages

No packages published