Intermediate Collection Flashcards

1
Q

What are the differences between null and undefined?

A

Undefined means, the variable exists but has not been given a value and is there for automatically assigned the value of undefined. JavaScript has a global variable undefined whose value is “undefined” and typeof undefined is also “undefined”. Remember, undefined is not a constant or a keyword. undefined is a type with exactly one value: undefined. Assigning a new value to it does not change the value of the type undefined.

Null means empty or non-existent value which is used by programmers to indicate “no value”. null is a primitive value and you can assign null to any variable. null is not an object, it is a primitive value. For example, you cannot add properties to it. Sometimes people wrongly assume that it is an object, because typeof null returns “object”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the differences between == and ===?

A

== will not check types and === will check whether both sides are of same type. So, == is tolerant. But under the hood it converts to its convenient type to have both in same type and then do the comparison.

=== compares the types and values. Hence, if both sides are not same type, answer is always false. For example, if you are comparing two strings, they must have identical character sets. For other primitives (number, boolean) must share the same value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the rules for implicit coercion?

A

If both operands are same type use ===

undefined == null

If one operands is string another is number, convert string to number

If one is boolean and another is non-boolean, convert boolean to number and then perform comparison

While comparing a string or number to an object, try to convert the object to a primitive type and then try to compare

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How would you compare two objects in JavaScript?

A

Equality check will check whether two objects have same value for same property. To check that, you can get the keys for both the objects. If the number of properties doesn’t match, these two objects are not equal. Secondly, you will check each property whether they have the same value. If all the properties have same value, they are equal.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the falsy values in JavaScript?

A

In javascript 6 things are falsy and they are- false, null, undefined, ‘’, 0, NaN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Is ‘false’ is false?

A

No. Because, it’s a string with length greater than 0. Only empty string is false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Is ‘ ‘ is false?

A

No. Because, it’s not an empty string. There is a white space in it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Is {} considered a falsy value?

A

No. It’s an object. An object without any property is an object can’t be falsy.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Is [] considered a falsy value?

A

No. It’s an array which is also an object, it just doesn’t have any values assigned yet.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

We know that ‘’ is falsy. What about new String(‘’)?

A

new String(“”) is not considered falsy. Though you are passing empty string to the string constructor, it is creating an String object. More precisely a instance of String object. It becomes an object. Hence, it is not false. so, it is truthy.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Is new Boolean(false) falsy?

A

No, we are creating a new instance of the Boolean object. Objects are truthy

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Is Boolean(function(){}) falsy?

A

No, we are passing an empty object(truthy) to the Boolean which will result in a truthy value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Is Boolean(/foo/) truthy?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is true%1

A
  1. When you are trying to find reminder of true, true becomes 1 and reminder of 1 while dividing by 1 is 0. you will get same result if you do false%1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is ‘‘%1

A

0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly