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 !!!

https://www.icodeeveryday.com/



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();
Enter fullscreen mode Exit fullscreen mode

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

        Const newarray = [...sheeps];
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

It's also similar to unary + operator

--5 gives 5
-true gives -1
Enter fullscreen mode Exit fullscreen mode

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

!!hello == true
Enter fullscreen mode Exit fullscreen mode

Answer is true

5)What is the output ?

   var x=9;
   x= x++;
  console.log(x);
Enter fullscreen mode Exit fullscreen mode

Answer is 9

6) Different ways to copy an object into another?

copy mainobj object into copyobj

let copyobj= Object.assign({}, mainobj);
Enter fullscreen mode Exit fullscreen mode

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));
Enter fullscreen mode Exit fullscreen mode

8) Write a code to format the array1 into array2

array1 = [
        {Key:"name", value:"Pravin"},
        {key:"age", value:26},
        {key:"address", value:"Nepal"}
]
Enter fullscreen mode Exit fullscreen mode

and get an output as:

array2 = [
        {name:"Pravin"},
        {age:26},
        {address:"Nepal"}
]
Enter fullscreen mode Exit fullscreen mode

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 = array1.map(elem=>{
let temp_obj = {};
temp_obj[elem.key] =elem.value;
return temp_obj;
});

console.log(array2);
Enter fullscreen mode Exit fullscreen mode

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

name =[Tommy, Romanu, Pravin, Omar'];
filtername(name, 'om');
Enter fullscreen mode Exit fullscreen mode

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));
Enter fullscreen mode Exit fullscreen mode

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

Top comments (4)

Collapse
 
mandelbrotman profile image
MandelbrotMan

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 {}

Collapse
 
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.

Collapse
 
jeromedeleon profile image
Jerome De Leon • Edited

Thanks for the tips! For question #8, you could write it in one line:

array1.map(({ key, value}) => ({ [key]: value }));

For question #9,

const filterName = (names, query) => names.filter(name => new RegExp(query, "i").test(name));
Collapse
 
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;
},{});