DEV Community

Cover image for 10 Interview Questions for Javascript Developer
Pravin Poudel
Pravin Poudel

Posted on

10 Interview Questions for Javascript Developer

This post is on 10 questions for junior developers out there and for those who are looking for or having an interview.

I also have a few articles in detail for an interview on my new Website and Follow me on twitter where i will be posting cool and short programming codes on this quarantine.
Go and read !!! It's always good to learn !!!

Let's Start:

1) How to copy or clone an array?

In the old way, we can copy an array with slice().

const newaaray= oldarray.slice();

In a new way i.e after ES6, we can copy array with the spread operator

        Const newarray = [...sheeps];

2)Why can’t we use = to copy an array like (new_variable = existing_array) ?

Because an array are reference value in javascript. When we write newarray = oldarray, we don’t create new array rather it's reference that point to the same memory location.

3)How do you check if a number is of number type or not in JS?

if(typeof variable === number && !isNaN(variable))

Remember that typeof variable ==='number' doesn't guarantee that variable is number because typeof NaN is also a number.

4) What is the output of +‘12’?

The result is number 12 of type number because the unary operator tries to convert all strings, boolean and null to number.

More example,

+-3 gives -3
+0xFF  gives output 255
+false  gives 0
+Infinity gives Infinity
+infinity gives NaN
+function gives NaN 

It's also similar to unary + operator

--5 gives 5
-true gives -1

If the interviewer is freak, the output of code below can be asked:
What does this return

!!hello == true

Answer is true

5)What is the output ?

   var x=9;
   x= x++;

Answer is 9

6) Different ways to copy an object into another?

copy mainobj object into copyobj

let copyobj= Object.assign({}, mainobj);

but:this is also a shallow copy.
I don't know if there is another way, for deep copying or cloning i use stringify.

let newObj = JSON.parse(JSON.stringify(obj));

8) Write a code to format the array1 into array2

array1 = [
        {Key:"name", value:"Pravin"},
        {key:"age", value:26},
        {key:"address", value:"Nepal"}

and get an output as:

array2 = [

There are a lot of ways to this. I am going to use map() to this array.

let array1 = [
        {key:"name", value:"Pravin"},
        {key:"age", value:26},
        {key:"address", value:"Nepal"}

let array2 =>{
let temp_obj = {};
temp_obj[elem.key] =elem.value;
return temp_obj;


9)Create a function filtername with the first parameter array and second parameter search query.

name =[Tommy, Romanu, Pravin, Omar'];
filtername(name, 'om');

condition: It should return an array with element of name which has 'om' in it.
In this output must be ['Tommy', 'Romanu', 'Omar'].

10)How do you clone a JSON object?

My answer is stringify.I don’t know if there is another way.Comment your way of doing.

let old_json = old json ;
Let new = JSON.parse(JSON.stringify(old));

Thanks for reading.
Take Care and Stay Safe. Help others in need.
Bye !!! See you soon :D

Top comments (4)

mandelbrotman profile image

Thank you Pravin this was very informative. If you don't mind I just want to point out somethings to edit +‘-3’ gives +3 should be -3. Also the object.assign will return a deep copy if you pass an empty object such as {}

voralagas profile image
Pravin Poudel • Edited

Thanks for that its -3. It's a typo mistake. No!! Object. assign() is shallow copying for an abject with object inside. It won't be able to copy object inside an object rather it copy the reference of an inside object.

jeromedeleon profile image
Jerome De Leon • Edited

Thanks for the tips! For question #8, you could write it in one line:{ key, value}) => ({ [key]: value }));

For question #9,

const filterName = (names, query) => names.filter(name => new RegExp(query, "i").test(name));
omarkhatibco profile image
Omar Khatib

8) Write a code to format the array1 into array2

you could use array.reduce to solve it, like this

const array2 = array1.reduce((obj,{key,value})=>{
obj[key] = value;
return obj;