Javascript Flashcards

1
Q

What are the differences between null and undefined?

A

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”.

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

=== compares the types and values

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

How would you compare two objects in JavaScript?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
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
5
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
6
Q

Is {} truthy or falsey

A

true. 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
7
Q

Is [] truthy or falsey

A

truthy. It’s an array object (array is child of object) is truthy.

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

As [] is truthy, []==true should also be true. right?

A

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.

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

If you want to use an arbitrary object as value of this, how will you do that?

A

There are at least three different ways to doing this by using bind, call and apply.

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

How can I tell if 2 is passed as a parameter of a function?

A

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.

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

What is typeof []

A

Object. Actually Array is derived from Object. If you want to check array use Array.isArray(arr)

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

What is typeof arguments

A

Object. arguments are array like but not array. it has length, can access by index but can’t push pop, etc.

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

What is 2+true

A
  1. 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is ‘6’+9

A
  1. 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”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the value of 4+3+2+”1”

A

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.

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

What is the value of “1”+2+4

A

“124”. For this one “1” + 2 will produce “12” and “12”+4 will generates “124”.

17
Q

What is the value of -‘34’+10

A

-24. minus(-) in front of a string is an unary operator that will convert the string to a number and will make it negative. Hence, -‘34’ becomes, -34 and then plus (+) will perform simple addition as both the operands are number.

18
Q

What is the value of +’dude’

A

NaN. The plus (+) operator in front of a string is an unary operator that will try to convert the string to number. Here, JavaScript will fail to convert the “dude” to a number and will produce NaN.

19
Q

If you have var y = 1, x = y = typeof x; What is the value of x?

A

“undefined”

20
Q

for var a = (2, 3, 5); what is the value of a?

A
  1. The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand.
21
Q

for var a = (1, 5 - 1) * 2 what is the value of a?

A

8

22
Q

What is the value of !’bang’

A

false. ! is NOT. If you put ! in front of truthy values, it will return false. Using !! (double bang) is a tricky way to check anything truthy or falsy by avoiding implicit type conversion of == comparison.

23
Q

What is the value of parseFloat(‘12.3.4’)

A

12.3

24
Q

What is the value of Math.max([2,3,4,5]);

A

NaN. Function should be written Math.max(…[2,3,4,5]);

25
Q

If var a = 2, b =3 what would be value of a && b

A

3

26
Q

Does JavaScript pass parameter by value or by reference?

A

Primitive type (string, number, etc.) are passed by value and objects are passed by reference. If you change a property of the passed object, the change will be affected. However, you assign a new object to the passed object, the changes will not be reflected.

27
Q

How would you implement currying for any functions?

A

Curring is partial invocation of a function. Currying means first few arguments of a function is pre-processed and a function is returned. The returning function can add more arguments to the curried function.

function addBase(base){
  return function(num){
    return base + num;
  }
}

var addTen = addBase(10);
addTen(5); //15
addTen(80); //90
addTen(-5); //5

28
Q

What’s the difference between undefined and not defined in JavaScript

A

In JavaScript if you try to use a variable that doesn’t exist and has not been declared, then JavaScript will throw an error var name is not defined and the script will stop execute thereafter. But If you use typeof undeclared_variable then it will return undefined.

var x is a declaration because you are not defining what value it holds yet, but you are declaring its existence and the need for memory allocation.

var x = 1 is both declaration and definition (also we can say we are doing initialisation)

29
Q

What is the drawback of declaring methods directly in JavaScript objects?

A

One of the drawback of declaring methods directly in JavaScript objects is that they are very memory inefficient. When you do that, a new copy of the method is created for each instance of an object.

30
Q

What is “closure” in javascript?

A

A closure is a function defined inside another function (called parent function) and has access to the variable which is declared and defined in parent function scope.

The closure has access to the variable in three scopes:

Variable declared in his own scope
Variable declared in parent function scope
Variable declared in the global namespace

31
Q

What is the DOM?

A

The Document Object Model (DOM) is a programming interface for HTML and XML documents. It represents the page so that programs can change the document structure, style, and content. The DOM represents the document as nodes and objects. That way, programming languages can connect to the page.

A Web page is a document. This document can be either displayed in the browser window or as the HTML source. But it is the same document in both cases. The Document Object Model (DOM) represents that same document so it can be manipulated. The DOM is an object-oriented representation of the web page, which can be modified with a scripting language such as JavaScript.