Tricky Javascript Code Snippets Asked In the Interview

Full Stack Developer | Freelancer | JavaScript | React | Nodejs.
Loves sharing knowledge through technical articles.
Dev.to: https://dev.to/myogeshchavan97
https://linktr.ee/myogeshchavan97
Search for a command to run...

Full Stack Developer | Freelancer | JavaScript | React | Nodejs.
Loves sharing knowledge through technical articles.
Dev.to: https://dev.to/myogeshchavan97
https://linktr.ee/myogeshchavan97
Yet again , Thanks for such a great article yogesh ... May i know how it is treated as no reassignment of number in this case .... as number is declared with const ... Thanks :)
const number = 1;
const result = (function (number) {
delete number;
return number;
})(10);
kishore kumar const is considered as block scoped.
We can declare a const variable with the same name in different blocks. Declaring a function creates a separate block scope so there is no reassignment error
ahh makes sense ... Thanks yogesh for your kind exp :)
Nice article as usual Yogesh Chavan. I love these little test snippets which I'm now starting to get better at solving since working on interview prep.
Thank you so much Kieran Roberts and all the best for your interview 👍
In this tutorial, you will build a Task Management App using Next.js 16 and Prisma 7 from scratch. By creating this app, you will learn: How to set up Prisma 7 with the new Rust-free architecture How to configure a database with Prisma's driver ada...

If you're a React/Next.js developer who diligently updated your packages last week after the critical React2Shell vulnerability (CVE-2025-55182), I have some frustrating news: you need to update again. Security researchers, while probing the patches ...

Is your React app dragging its feet, but you can’t pinpoint the cause? You might think you’re following the best practices, but some common development patterns are actually degrading your app’s performance behind the scenes. In this article, we’ll u...

How to Use Separate SSH Keys for Company and Personal GitHub Accounts Managing multiple GitHub accounts is a common challenge for developers who juggle both personal and professional projects. It becomes even more complex when you want to use differe...

Have you ever built a React app only to realize that users from different countries can't use it effectively because it only supports English? In this tutorial, you'll learn how to build truly global React application using internationalization (i18n...

In this article, we will see some of the most famous interview code snippets that are asked to check your knowledge about Javascript concepts.
Let's get started.
Predict the output of the below code:
const user = {
name: 'Raj',
location: {
city: 'NY',
state: 'NY'
}
};
const copy = Object.assign({}, user);
// OR
// const copy = { ...user };
copy.location.city = 'Albany';
console.log('original: ', user.location);
console.log('copy:', copy.location);
The output of the above code is:
original: {
city: 'Albany',
state: 'NY'
}
copy: {
city: 'Albany',
state: 'NY'
}
The reason for the changed original object is because when we use Object.assign or spread operator, it only does shallow copy, which means while creating a copy of the object, properties of only the first level are copied
And if there are nested properties then only their reference is copied which means the copied reference still refers to the original place where the object is stored.
So in our case, the location property will still refer to the original object which is a user object in our case.
var number = 10;
var display = function () {
console.log(number);
var number = 20;
};
display();
The output of the above code is not 10 but it’s undefined
Why?
This is because of hoisting in Javascript.
So the above code will be converted to the below code:
var number = 10;
var display = function () {
var number;
console.log(number);
number = 20;
};
display();
As you can see only the declaration is moved to the start of the function and the value is not hoisted so the console.log prints undefined because number does not have any value assigned inside the function before the console.log statement.
const number = 1;
const result = (function () {
delete number;
return number;
})();
console.log(result);
This code will not give any error because we are using delete on number but it will print the value 1 as the output.
This syntax of calling the function directly is known as IIFE (Immediately Invoked Function Expression).
The delete operator is used to delete the property of an object. Here, number is not an object but it's a primitive type so it will not throw an error but the function will return the original value of the variable which is 1 which is in the scope of the console.log statement.
const number = 1;
const result = (function (number) {
delete number;
return number;
})(10);
console.log(result);
The output of the above code is 10.
In this code, we are passing the value 10 as the input to the function. But again inside the function number is just a local primitive type of variable so delete will not make any changes to the number and the value 10 passed to the function will be returned from the function.
function display() {
var a = b = 10;
}
display();
console.log('b', typeof b === 'undefined');
console.log('a', typeof a === 'undefined');
The output of the above code is:
b false
a true
This is because the assignment operator has a right to left associativity in Javascript which means it will be evaluated from right to left so first, b will be assigned the value 10 and then it’s assigned to a.
Therefore the following code,
function display() {
var a = b = 10;
}
is same as
function display() {
var a = (b = 10);
}
which is the same as
function display() {
b = 10;
var a = b;
}
So b becomes a global variable because there is no var keyword before it and a becomes a local variable. Therefore, outside the function, only bis available so typeof a === 'undefined' comes as true and typeof b === 'undefined' comes as false.

If we execute the above code in strict mode as shown below:
'use strict';
function display() {
var a = b = 10;
}
display();
console.log('b', typeof b === 'undefined');
console.log('a', typeof a === 'undefined');
It will throw an error because b becomes a global variable and strict mode does not allow creating global variables so you will get an error when you execute this code.
Starting with ES6, there are many useful additions to JavaScript like
You can learn everything about all the ES6+ features in detail in my Mastering Modern JavaScript book.
Check out free preview contents of the book here.
Also, you can check out my free Introduction to React Router course to learn React Router from scratch.
Want to stay up to date with regular content regarding JavaScript, React, Node.js? Follow me on LinkedIn.