Skip to content

Explore the world of JavaScript through practical output-based questions

Notifications You must be signed in to change notification settings

surbhidighe/Javascript-Output-Based-Questions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

18 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

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(arr1==arr2) : 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

32. What will be the output

const arr = [10, -1, 2];
arr.sort((a, b) => a - b);
console.log(arr);
View Answer
  • Output : [-1, 2, 10]
  • Reason : The compare function (a, b) => a - b sorts the numbers numerically in ascending order.

πŸ” Scroll to Top

33. What will be the output

const arr = [11, 0, '', false, 2, 1];
const filtered = arr.filter(Boolean);
console.log(filtered);
View Answer
  • Output : [11, 2, 1]
  • Reason : filter(Boolean) removes all falsy values (0, "" (empty string), false, null, undefined, and NaN) from the array and keeps truthy ones.

πŸ” Scroll to Top

34. What will be the output

var x = 0;
var y = 10;
if(x){
  console.log(x);
}
if(y){
  console.log(y);
}
View Answer
  • Output : 10
  • Reason : x = 0 is falsy and doesn't trigger the console.log(x), while y = 10 is truthy and triggers the console.log(y).

πŸ” Scroll to Top

35. What will be the output

const obj = {
var1: 1,
var2: 2
};
const { var1, var2 } = obj;
console.log(var1, var2);
View Answer
  • Output : 1, 2
  • Reason : Object destructuring extracts the values of var1 and var2 from obj object and prints them using console.log(var1, var2)

πŸ” Scroll to Top

36. What will be the output

const user = { 
name: "Surbhi dighe", 
country: "India" 
};
const { name: fullname, country } = user;
console.log(fullname);
console.log(name);
View Answer
  • Output : Surbhi Dighe, ReferenceError: name is not defined
  • Reason for console.log(fullname) : The name property from user is assigned to a local variable fullname.
  • Reason for console.log(name) : It gives an error because name was assigned to a local variable fullname and therefore name is not directly accessible.

πŸ” Scroll to Top

37. What will be the output

const person = {
  firstName: 'Surbhi',
};
const { lastName="dighe" } = person;
console.log(lastName);
View Answer
  • Output : dighe
  • Reason : The lastName property is not defined in the person object and the destructuring syntax provides a default value ("dighe") to be used when the property is missing.

πŸ” Scroll to Top

38. What will be the output

const person = {
  firstName: 'Surbhi',
};
const { firstName="Henry"} = person;
console.log(firstName);
View Answer
  • Output : Surbhi
  • Reason : The `firstName` property in the `person` object has the value 'Surbhi'. The default value "Henry" is ignored because it only applies when the property does not exist or is `undefined`

πŸ” Scroll to Top

39. What will be the output

var a = 10;
let a = 20;
console.log(a)
View Answer
  • Output : SyntaxError: Identifier 'a' has already been declared
  • Reason : In Javascript, we cannot redeclare a variable with let if it has already been declared in the same scope.

πŸ” Scroll to Top

40. What will be the output

const arr = ["A","B","C","D","E"]
console.log(Object.keys(arr)); 
View Answer
  • Output : [ '0', '1', '2', '3', '4' ]
  • Reason : In JavaScript, arrays are a special type of object. Object.keys() on an array returns an array of strings representing the indices of the array elements.

πŸ” Scroll to Top