You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
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.
for(vari=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.
for(leti=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.
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.
Reason : Both the variables are declared using "var" keyword with the same name "a". The second variable declaration will override the first variable declaration.
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.
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.
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.
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".
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.
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.
28. What will be the output (shallow copy of an object)
constuserDetails={firstName:"Surbhi",lastName:"Dighe",age:20,address:{city:"Hyderabad",country:"India",},};letcloneUserDetails={ ...userDetails};//Updating original objectuserDetails.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".
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.
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
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.
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.
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`
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.
Output : Output of the first console log will be "Updated". Output of the second console log will also be "Updated".
Reason : JS does not allow true pass by reference. It uses call by value for primitives (numbers, strings, booleans, null, undefined and symbols) and call by sharing for objects and arrays. If you modify an object's properties inside a function, the changes will reflect outside the function but if you reassign the object completely inside the function, the original reference will remain unchanged outside.
Output : First console log will output "1". Second console log will output "1001".
Reason : The spread operator provides a shallow copy operation and any objects that are deeply nested in the variable a will still be shared between a as well as b. To make an actual deep copy, use the method structuredClone(a)
Output : The console will output in this order -> Start, End, Promise, setTimeout
Reason : The execution order is Synchronous code first, then Microtasks run and the Microtask queue is emptied, then the Macrotasks run in the Task queue/ Callback queue. All the callbacks in the .then(), .catch() and .finally() get into the microtask queue and the other asynchronous operations, go into the WebAPIs first and when they are completed, the callbacks in them go to task queue.
lettext;switch(1){case0:text="This is zero";break;case1:text="This is one";case2:text="This is two";break;default:text="No matches found!";}console.log(text);
View Answer
Output : This is two
Reason : switch(1) means JS looks for a case that matches 1. It finds case 1 & sets text as "This is one". But there is no break, so it keeps going (falls through) into case 2 & overwrites the value.
constuser={name:'Aman Bhoria!',logMessage(){console.log(this.name);// What is logged?}};setTimeout(user.logMessage,1000);
View Answer
Output : undefined
Reason : We've passed the reference in setTimeout not the actual function so as a result it doesn't have the user's context while executing. To get the name we've to pass a callback like: setTimeout(() => user.logMessage(), 1000);
Reason : When two or more objects are merged: If keys conflict (i.e., the same key exists in multiple objects), the value from the later object overwrites the earlier one.
Reason :In JavaScript, using an object as a key in a normal object turns it into a string. That string is usually "[object Object]". So, two different objects like b and c become the same key -> a[b] = 111 & a[c] = 222 becomes a["[object Object]"] = 111 & a["[object Object]"] = 222. Hence, the second value (222) replaces the first one.
Reason :In non-strict mode, the arguments object (an array-like object) holds the values passed to the function. When you change the function parameters, the corresponding values in the arguments object are updated as well. In strict mode, this link is removed. As a result, function prints the original values that were passed.