Sometimes JavaScript is Tricky.
Tags: javascript, webdev, career, html
Published: 2021-01-30 19:07:56

When I was new in Web Development, I faced many difficulties while learning JavaScript, but after regular practice, my views changed.

To be honest, still  Javascript changes my perspective, but it also gives me more strength to work hard on it.

I accumulated some questions from the internet and I am sure this will change your views too.

But before going straight to the questions:-
### *<mark>"The Expert in anything was once a beginner"</mark>* ###

![](https://media.giphy.com/media/Ln2dAW9oycjgmTpjX9/giphy.gif)
### **Q1- What is typeof []** ###
> Answer: Object. Actually Array is derived from Object. If you want to check array use Array.isArray(arr)

<br>

### **Q2- What is 2+true** ###
> Answer: 3. The plus operator between a number and a boolean or two boolean will convert boolean to number. Hence, true converts to 1 and you get result of 2+1.

</br>

### **Q3- What is '6'+9** ###
> Answer: 69. If one of the operands of the plus (+) operator is string it will convert other number or boolean to string and perform a concatenation.

<br>

### **Q4- What's the output?** ###
```javascript
let a = 0;
console.log(a++);
console.log(++a);
console.log(a);
```
>The postfix unary operator(a++) 
First it returns 0 and after that increment a by 1 means now a=1.

>The prefix unary operator(++a)
First increment by 1 and then return means now a=2

>Answer: 0 2 2

<br>

### **Q5- What's the output?** ###
```javascript
function myAge(...args) {
  console.log(typeof args);
}

myAge(23);
```
>Answer: Object.The rest parameter (...args) lets us "collect" all remaining arguments into an array. An array is an object, so typeof args returns "object".

<br>

### **Q6- What's the output?** ###
```javascript
const confusing = { a: 'one', b: 'two', a: 'three' };
console.log(confusing);
```
>Answer: If you have two keys with the same name, the key will be replaced. It will still be in its first position, but with the last specified value.

<br>

### **Q7- What's the output?** ###
```javascript
for (let i = 1; i < 5; i++) {
  if (i === 3) continue;
  console.log(i);
}

```
>Answer: 1 2 4.The continue statement skips an iteration if a certain condition returns true.

<br>

### **Q8- What's the output?** ###
```javascript
var a=(2,3,4,5,6,7);
console.log(a);
```
>Answwer:7.The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand.

<br>

### **Q9- What is the value of !'Sleep'?** ###
>Answer:False. ! is NOT. If you put ! in front of truthy values, it will return false.

<br>

### **Q10-What the logged output when you click the paragraph?** ###
```html
<div onclick="console.log('div')">
  <p onclick="console.log('p')">
    Click here!
  </p>
</div>
```
>Answer: p div. If we click p, we see two logs: p and div. During event propagation, there are 3 phases: capturing, target, and bubbling. By default, event handlers are executed in the bubbling phase (unless you set useCapture to true). It goes from the deepest nested element outwards.

<br>

### **Q11- What is the output?** ###
```javascript
const myName = { name: 'Rajat Arya' };

function hello(age) {
  return `${this.name} is ${age}`;
}

console.log(hello.call(myName, 23));
console.log(hello.bind(myName, 23));

```
>Answer: Rajat Arya is 23  
>function hello(age) {
  return `${this.name} is ${age}`;
}

>Explanation:- With both, we can pass the object to which we want the <b>'this'</b> keyword to refer to. However,<b> .call</b> is also executed immediately!
<b>.bind </b> returns a copy of the function, but with a bound context! It is not executed immediately.

<br>

### **Q12- What's the output?** ###
```javascript
function think() {
  return (() => 0)();
}

console.log(typeof think());


```
>Answer:number The <b>think</b> function returns the returned value of the immediately invoked function expression (IIFE). This function returned 0, which is type "number".

<br>

### **Q13- What's the output?** ###
```javascript
const numbers = [1, 2, 3];
numbers[10] = 11;
console.log(numbers);
```
>Answer:When you set a value to an element in an array that exceeds the length of the array, JavaScript creates something called "empty slots". These actually have the value of undefined, but you will see something like:
<br>
[1, 2, 3, empty x 7, 11]

<br>

### **Q14-What's the Output?** ###
```javascript
console.log(typeof typeof 1);
```
>Answer: string. From right typeof 1 returns "number". typeof "number" returns "string"

<br>

### **Q15-What's the Output?** ###
```javascript
var myName=[...'Rajat'];
console.log(myName)
```
>Answer:<b> ["R","a","j","a","t"]</b>
A string is an iterable. The spread operator maps every character of an iterable to one element.