Junior Garcia@jrgarciadev
Top JavaScript Interview Questions
A selection of crucial JavaScript interview topics to test your skills. Prepare for your next interview with these top questions, designed to evaluate your knowledge and problem-solving abilities.
Here's a rephrased and reordered version of your questions to make them easier to understand and ordered by difficulty and similarity:
Basic Questions
Question 1. What will be the output of the following code?
var output = (function(x) { delete x; return x; })(0); console.log(output);
Answer
The output will be 0
. The delete
operator is used to remove properties from objects. Here, x
is a local variable, not an object property, so the delete
operator has no effect on it.
Question 2. What will be the output of the following code?
var x = 1; var output = (function() { delete x; return x; })(); console.log(output);
Answer
The output will be 1
. The delete
operator is used to remove properties from objects. Here, x
is a global variable of type number
, not an object property, so the delete
operator has no effect on it.
Question 3. What will be the output of the following code?
var x = { foo : 1 }; var output = (function() { delete x.foo; return x.foo; })(); console.log(output);
Answer
The output will be undefined
. The delete
operator removes the foo
property from the x
object. After deletion, x.foo
no longer exists, so it returns undefined
.
Intermediate Questions
Question 4. How can you check if an object is an array in JavaScript?
Answer
You can check if an object is an array using several methods:
- Using
Array.isArray
:
Array.isArray(arrayList);
- Using
Object.prototype.toString
:
if(Object.prototype.toString.call(arrayList) === '[object Array]') { console.log('Array!'); }
- Using jQuery (if available):
if($.isArray(arrayList)) { console.log('Array'); } else { console.log('Not an array'); }
On this page
Page 1 of 3