10 Interview Questions for Javascript Developer
Tags: javascript, webdev, productivity
Published: 2020-04-05 17:17:42

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 <a href="https://www.icodeeveryday.com/" target="_blank">my new Website</a> and Follow me on <b><a href="https://twitter.com/voralagas">twitter</a></b> 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/
<a href="https://www.icodeeveryday.com/" target="_blank" />
<img src="https://dev-to-uploads.s3.amazonaws.com/i/00xyci6593h8h8wl8czx.png" />
</a>
<u><h2>Let's Start:</h2></u>
<h3> 1) How to copy or clone an array?</h3>
 In the old way, we can copy an array with slice().
```javascript	
const newaaray= oldarray.slice();
```
  In a new way i.e after ES6, we can copy array with the spread operator
```javascript
		Const newarray = [...sheeps];
```
<h3> 2)Why can’t we use = to copy an array like (new_variable = existing_array) ?</h3>
Because an array are reference value in javascript. When we write <i>**newarray = oldarray**</i>, we don’t create new array rather it's reference that point to the same memory location.

<h3>3)How do you check if a number is of number type or not in JS?</h3>
```javascript
if(typeof variable === ‘number’ && !isNaN(variable))
```
Remember that <i>typeof variable ==='number'</i> doesn't guarantee that variable is number because  typeof NaN is also a number. 

<h3>4) What is the output of +‘12’?</h3>
The result is number 12 of type number because the unary operator tries to convert all strings, boolean and null to number.

**More example,**
```javascript
+‘-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** 
```javascript
-‘-5’ gives 5
-true gives -1
```

**If the interviewer is freak, the output of code below can be asked:**
<i>What does this return</i>
```javascript
!!hello == true
```
Answer is <i>true</i>

<h3>5)What is the output ?</h3>
```javascript 
   var x=9;
   x= x++;
  console.log(x);
```


Answer is 9

<h3>6) Different ways to copy an object into another?</h3>
**copy mainobj object into copyobj**
```javascript
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.
```javascript
let newObj = JSON.parse(JSON.stringify(obj));
```

<h3>8) Write a code to format the <i>array1</i> into <i>array2</i></h3>
```javascript
array1 = [
        {Key:"name", value:"Pravin"},
        {key:"age", value:26},
        {key:"address", value:"Nepal"}
]
```

**and get an output as:**
```javascript
array2 = [
        {name:"Pravin"},
        {age:26},
        {address:"Nepal"}
]
```
**There are a lot of ways to this. I am going to use map() to this array.**
```javascript
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);
```

<h3>9)Create a function filtername with the first parameter array and second parameter search query.</h3>
```javascript
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'].

<h3>10)How do you clone a JSON object?</h3>
My answer is stringify.I don’t know if there is another way.Comment your way of doing. 
```javascript
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