Intermediate Collection Flashcards
What are the differences between null and undefined?
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”
What are the differences between == and ===?
== 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.
What are the rules for implicit coercion?
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 would you compare two objects in JavaScript?
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.
What are the falsy values in JavaScript?
In javascript 6 things are falsy and they are- false, null, undefined, ‘’, 0, NaN
Is ‘false’ is false?
No. Because, it’s a string with length greater than 0. Only empty string is false.
Is ‘ ‘ is false?
No. Because, it’s not an empty string. There is a white space in it.
Is {} considered a falsy value?
No. It’s an object. An object without any property is an object can’t be falsy.
Is [] considered a falsy value?
No. It’s an array which is also an object, it just doesn’t have any values assigned yet.
We know that ‘’ is falsy. What about new String(‘’)?
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.
Is new Boolean(false) falsy?
No, we are creating a new instance of the Boolean object. Objects are truthy
Is Boolean(function(){}) falsy?
No, we are passing an empty object(truthy) to the Boolean which will result in a truthy value.
Is Boolean(/foo/) truthy?
Yes
What is true%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
What is ‘‘%1
0