Data types Flashcards
(44 cards)
How do primitive methods work in general?
The JavaScript engine wraps a temporary object around the primitive that enables those methods.
What is the “lazy” syntax for writing big numbers like 1,000,000 or small numbers like 0.0004?
Appending the letter “e” with the zeroes or negative zeroes count.
E.g. 1e6 === 1,000,000
What are common numeral systems in JavaScript?
Besides decimal (10): hex (16), octal (8) and binary (2)
How to write hexdecimal numbers?
Starting the number with 0x
How to write octal numbers?
Starting the number with 0o
How to write binary numbers?
Starting the number wie 0b
How to transform a number from one numeral system into another?
parseInt(number, base)
How to transform a string to a number in a specific numeral system?
number.toString(base)
How to convert values like 12pt or 100px to numbers?
parseInt() or parseFloat()
What are the limitations of parseInt() and parseFloat()?
The return the numbers from left to right to the first not-number character. Everything else get’s ignored.
Which operations provide rounding of numbers?
Math.floor() rounds down to nearest integer; Math.ceil() rounds up to nearest integer; Math.round() rounds to nearest integer; Math.trunc() removes everything after the decimal point
toFixed(n) rounds the number to n digits after the decimal point (but returns a string)
How to check for NaN?
isNaN() - NaN is special, is does not equal to anything so comparison doesn’t work
What does isFinite(value) do?
Converts value to number an returns true if number is not Infinity, -Infinity or NaN
Why do you pay attention when working with fractions?
Fractions are imprecise, that’s why 0.1 + 0.2 == 0.3 // false
Why is this while loop infinite?
let i = 0; while ( != 10) { i += 0.2; }
i would never be exactly 10 because of imprecise fractions in JavaScript
How to wrap a string?
With single quotes ‘ ‘, double quotes “ “ or backticks ` `
What is the difference between single quotes, double quotes and backticks?
Single and double quotes are the same. Backticks are a new ES6 feature and allow multiple lines and embedded expressions with ${…}
How are strings in JavaScript encoded?
UTF-16
How can we use special characters in strings?
By escaping them with \ or using unicode characters with \u…
How to access a single character in a string?
string[]
How to get a substring?
.slice(start [, end]) or .substring(start [,end])
slice includes start but not end
substring only string between start and end
How to lowercase or uppercase a string?
.toLowerCase and .toUpperCase
How to find substrings?
.indexOf(substr, pos) -> returns position where substr starts
.includes(substr) -> returns true/false
.startsWith(substr) -> returns true/false
.endsWith(substr) -> returns true/false
How does string comparison work?
Letter by letter which is in the alphabetical order (or better which numeric code in UTF-16 is smaller)
Capital letters are smaller than lowercase letters