Javascript Flashcards
How does Array.prototype.sort() elements in an array?
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.
What is the difference between two functions Number (value) and parseInt (value) in JavaScript?
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.