Javascript Flashcards
What are the differences between null and undefined?
JavaScript has two distinct values for nothing, null and undefined.
undefined means, value of the variable is not defined.
null means empty or non-existent value which is used by programmers to indicate “no value”.
What are the differences between == and ===?
== will not check types and === will check whether both sides are of same type
=== compares the types and values
How would you compare two objects in JavaScript?
JavaScript has two different approaches for testing equality. Primitives like strings and numbers are compared by their value, while objects like arrays, dates, and user defined objects are compared by their reference. This means it compares whether two objects are referring to the same location in memory.
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.
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 {} truthy or falsey
true. It’s an object. An object without any property is an object can’t be falsy.
Is [] truthy or falsey
truthy. It’s an array object (array is child of object) is truthy.
As [] is truthy, []==true should also be true. right?
Since left and right side of the equality are two different types, JavaScript can’t compare them directly . Hence, under the hood, JavaScript will convert them to compare. first right side of the equality will be cooereced to a number and number of true would be 1.
After that, JavaScript implementation will try to convert [] by usingtoPrimitive (of JavaScript implementation). since [].valueOf is not primitive will use toString and will get “”
Now you are comparing “” == 1 and still left and right is not same type. Hence left side will be converted again to a number and empty string will be 0.
Finally, they are of same type, you are comparing 0 === 1 which will be false.
If you want to use an arbitrary object as value of this, how will you do that?
There are at least three different ways to doing this by using bind, call and apply.
How can I tell if 2 is passed as a parameter of a function?
arguments is a local variable, available inside all functions that provides a collection of all the arguments passed to the function. arguments is not an array rather an array like object. It has length but doesn’t have the methods like forEach, indexOf, etc.
First convert arguments to an array by calling slice method on an array and pass arguments. After that simply use indexOf.
What is typeof []
Object. Actually Array is derived from Object. If you want to check array use Array.isArray(arr)
What is typeof arguments
Object. arguments are array like but not array. it has length, can access by index but can’t push pop, etc.
What is 2+true
- 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
What is ‘6’+9
- If one of the operands of the plus (+) operator is string it will convert other number or boolean to string and perform a concatenation. For the same reason, “2”+true will return “2true”
What is the value of 4+3+2+”1”
91 . The addition starts from the left, 4+3 results 7 and 7+2 is 9. So far, the plus operator is performing addition as both the operands are number. After that 9 + “1” where one of the operands is string and plus operator will perform concatenation.