Data types Flashcards

(44 cards)

1
Q

How do primitive methods work in general?

A

The JavaScript engine wraps a temporary object around the primitive that enables those methods.

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

What is the “lazy” syntax for writing big numbers like 1,000,000 or small numbers like 0.0004?

A

Appending the letter “e” with the zeroes or negative zeroes count.
E.g. 1e6 === 1,000,000

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

What are common numeral systems in JavaScript?

A

Besides decimal (10): hex (16), octal (8) and binary (2)

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

How to write hexdecimal numbers?

A

Starting the number with 0x

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

How to write octal numbers?

A

Starting the number with 0o

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

How to write binary numbers?

A

Starting the number wie 0b

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

How to transform a number from one numeral system into another?

A

parseInt(number, base)

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

How to transform a string to a number in a specific numeral system?

A

number.toString(base)

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

How to convert values like 12pt or 100px to numbers?

A

parseInt() or parseFloat()

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

What are the limitations of parseInt() and parseFloat()?

A

The return the numbers from left to right to the first not-number character. Everything else get’s ignored.

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

Which operations provide rounding of numbers?

A

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

How to check for NaN?

A

isNaN() - NaN is special, is does not equal to anything so comparison doesn’t work

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

What does isFinite(value) do?

A

Converts value to number an returns true if number is not Infinity, -Infinity or NaN

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

Why do you pay attention when working with fractions?

A

Fractions are imprecise, that’s why 0.1 + 0.2 == 0.3 // false

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

Why is this while loop infinite?

let i = 0;
while ( != 10) {
   i += 0.2;
}
A

i would never be exactly 10 because of imprecise fractions in JavaScript

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

How to wrap a string?

A

With single quotes ‘ ‘, double quotes “ “ or backticks ` `

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

What is the difference between single quotes, double quotes and backticks?

A

Single and double quotes are the same. Backticks are a new ES6 feature and allow multiple lines and embedded expressions with ${…}

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

How are strings in JavaScript encoded?

19
Q

How can we use special characters in strings?

A

By escaping them with \ or using unicode characters with \u…

20
Q

How to access a single character in a string?

21
Q

How to get a substring?

A

.slice(start [, end]) or .substring(start [,end])

slice includes start but not end
substring only string between start and end

22
Q

How to lowercase or uppercase a string?

A

.toLowerCase and .toUpperCase

23
Q

How to find substrings?

A

.indexOf(substr, pos) -> returns position where substr starts

.includes(substr) -> returns true/false
.startsWith(substr) -> returns true/false
.endsWith(substr) -> returns true/false

24
Q

How does string comparison work?

A

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

25
How to compare strings correct?
For different languages string comparison works with .localeCompare()
26
Explain str.trim()
Trims whitespace of string from beginning and end
27
Explain str.repeat(n)
Repeats the strings n times
28
How to declare an array?
let arr = [item1, item2, ...] ``` or rare syntax: let arr = new Array(item1, item2, ...) ```
29
What is special about new Array(2)?
It creates and empty array with length equals 2
30
Explain the length property?
Special property of arrays which give the length or, better the last numeric index plus one
31
What happens if we shorten the length property?
We truncate the array
32
How to add or remove items from an array?
push(...items) - add to the end pop() - remove from the end and return removed one shift(...items) - add to the beginning unshift() - remove from the beginning and return removed one
33
How to loop over an array?
for loop -> classic and fastests for of -> modern syntax for in -> possible but for objects optimized
34
Explain arr.splice()
Syntax: arr.splice(index[, deleteCount, elem1, ... elemN]) Starts at index, deletes deleteCount elements and adds elem1... elemN It is possible to only remove or only add.
35
Explain arr.slice()
Syntax: arr.slice(start, end) Returns a new strings, beginning at start and ending (not including) at end.
36
Explain arr.concat()
Syntax: arr.concat(arg1, arg2, ...) Joins multiple Arrays/items. Turns joined arrays in items of the new array.
37
Explain arr.forEach()
Syntax: arr.forEach(callback) Iterates over array and calls callback on every element.
38
Explain arr.indexOf(), arr.lastIndexOf() and arr.includes()
Searches for item (starting from position) and returns the position or true/false (includes)
39
Explain arr.find() and arr.findIndex()
Syntax: arr.find(callback(item, index, array)) Callback is called for each item, if it returns true the item is returned, else undefined. findIndex() returns the index value.
40
Explain arr.filter()
Syntax: arr.filter(callback(item, index, array)) arr.find() returns the first match, filter() returns all matches in a new array
41
Explain arr.map()
Syntax: arr.map(function(item, index, array) {}) Used to transform items in an array. Calls function on each element and returns array of results.
42
Explain arr.sort()
Returns the sorted array.
43
Explain arr.reverse()
arr.reverse() reverses the ordering of elements in an array
44
Explain str.split() and arr.join()
str. split(delimiter) splits a string after each delimiter and turns it into an array. arr. join(delimiter) turn a array into an string with delimiter between each element