Javascript Flashcards

1
Q

How does Array.prototype.sort() elements in an array?

A

If compareFunction(a, b) returns less than 0, sort a to an index lower than b (i.e. a comes first).

If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. Note: the ECMAscript standard does not guarantee this behavior, thus, not all browsers (e.g. Mozilla versions dating back to at least 2003) respect this.

If compareFunction(a, b) returns greater than 0, sort b to an index lower than a (i.e. b comes first).

compareFunction(a, b) must always return the same value when given a specific pair of elements a and b as its two arguments. If inconsistent results are returned, then the sort order is undefined.

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

What is the difference between two functions Number (value) and parseInt (value) in JavaScript?

A

Number(object) tries to convert the entire string to a number(which can also be a float) while parseInt(object) parses up to first non-digit and returns whatever value has been parsed.
eg:
Number(‘123num’) returns NaN
parseInt(‘123num’) returns 123

Also, the complete format of parseInt() is parseInt(object, radix) where radix defines the base of the integer to be parsed into.

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