Primitive Types Module #17 Flashcards

1
Q

How would you define a number using hexadecimal syntax?

A

Start with the 0x prefix.

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

How would you convert $10.00 so that the calculations are dependably exact?

A

Multiply by 100 ( 10 x 100 = 1000 ) Stripe uses this method and essentially dollar values are calculated in pennies. .01 === 1

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

How can you test for a “type” of a variable?

A
Use the "typeof" keyword. For example:
const age = Number( 36 )
typeof age //number
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can an you create an object from a number type?

A
Use the "new" keyword in front of the number. For Example: 
const age = new Number ( 36 )
typeof age //object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What can you use to determine the original number value of an object?

A

METHOD

Use the value of method. Example:
age.valueOf ( )

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

Define EPSILON.

A

EPSILON is the smallest interval between two numbers.

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

Define MAX_SAFE_INTEGER.

A

MAX_SAFE_INTEGER is the maximum integer value JavaScript can represent.

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

Define MAX_VALUE.

A

MAX_VALUE is the maximum positive value that JavaScript can represent.

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

Define MIN_SAFE_INTEGER.

A

MIN_SAFE_INTEGER is the minimum integer value JavaScript can represent.

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

Define MIN_VALUE.

A

MIN_VALUE is the minimum positive value JavaScript can represent.

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

Define NaN

A

A special value representing “not a number”.

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

METHODS: How is the NaN method expressed in JavaScript?

A

Number.isNaN(value) returns true if value is not a number

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

METHODS: How is the finite number method expressed?

A

Number.isFinite(value): returns true if value is a finite number.

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

METHODS: How is the integer method expressed ?

A

METHOD

Number.isInteger(value): returns true if value is an integer.

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

METHODS: How is the “safe” integer method expressed ?

A

METHOD

Number.isSafeInteger(value): returns true if value is a safe integer.

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

METHODS: How is the floating point number method expressed?

A

Number.parseFloat(value): converts value to a floating point number and returns it.

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

METHODS: How is the value of a floating point number converted into an integer expressed as a method?

A

Number.parseInt(value): converts value to an integer and returns it. It can also convert strings with words, extracting the first number, but the string must start with a number.

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

What kinds of methods can you use on a “Number Object”?

A

.toExponential( ), .toFixed( ), .toLocaleString( ), .toPrecision( ), .toString( ), .valueOf( )

19
Q

What’s another way to define a string other than quotes? What’s it called?

A

By using backticks this is called a template literal. This relieves the need to escape double or single quotes with backslashes.

20
Q

How would you insert a variable or expression into a string?

A

“By using ${ dollarsign-curly brace syntax with backticks arouund it all } Inside the braces you can add any JS expression.”

21
Q

What is charAt( i )

A
It is a method that returns the character at the position of i in a string. 
Examples:
"Flavio".charAt(0) //'F'
"Flavio".charAt(1) //'l'
"Flavio".charAt(2) //'a'
22
Q

What is concat (str)? What does it do?

A

Concatenates the current string with the string str. For example:
“Flavio”.concat(“ “).concat(“Copes”) //’Flavio Copes’

23
Q

What does endsWith(str) do?

A

Check if a string ends with the value of the string str. Example:
“JavaScript”.endsWith(“Script”) //true
“JavaScript”.endsWith(“script”) //false

24
Q

What does includes(str) do?

A

Check if a string includes the value of the string str.
“JavaScript”.includes(“Script”) //true
“JavaScript”.includes(“script”) //false
“JavaScript”.includes(“JavaScript”) //true
“JavaScript”.includes(“aSc”) //true
“JavaScript”.includes(“C++”) //false

includes() also accepts an optional second parameter, an integer which indicates the position where to start searching for:

“a nice string”.includes(“nice”) //true
“a nice string”.includes(“nice”, 3) //false
“a nice string”.includes(“nice”, 2) //true

25
Q

What does this return?

inexOf(str)

A

Gives the position of the first occurrence of the string str in the current string. Returns -1 if the string is not found.

Examples:
"JavaScript".indexOf("Script") //4
"JavaScript".indexOf("JavaScript") //0
"JavaScript".indexOf("aSc") //3
"JavaScript".indexOf("C++") //-1

You can pass a second parameter to set the starting point:

“a nice string”.indexOf(“nice”) !== -1 //true
“a nice string”.indexOf(“nice”, 3) !== -1 //false
“a nice string”.indexOf(“nice”, 2) !== -1 //true

26
Q

What does this method return?

lastIndexOf(str)

A

Gives the position of the last occurrence of the string str in the current string.

For example:
“JavaScript is a great language. Yes I mean JavaScript”.lastIndexOf(“Script”) //47
“JavaScript”.lastIndexOf(“C++”) //-1

27
Q

What does this return?

padEnd( )

A

METHOD

The purpose of string padding is to add characters to a string, so it reaches a specific length.

padEnd( ), introduced in ES2017, adds such characters at the end of the string.

padEnd(targetLength [, padString])

Sample usage:
padEnd( ) 	
‘test’.padEnd(4) 	‘test’
‘test’.padEnd(5) 	‘test ‘
‘test’.padEnd(8) 	‘test    ‘
‘test’.padEnd(8, ‘abcd’) 	‘testabcd’
28
Q

What does this return?

padStart( )

A

METHOD

Like padEnd( ), we have padStart( ) to add characters at the beginning of a string.

padStart(targetLength [, padString] )

Sample usage:
padStart( ) 	
‘test’.padStart(4) 	‘test’
‘test’.padStart(5) 	‘ test’
‘test’.padStart(8) 	‘    test’
‘test’.padStart(8, ‘abcd’) 	‘abcdtest’
29
Q

What does this method return?

repeat( )

A

METHOD

Introduced in ES2015, repeats the strings for the specificed number of times:

“Ho”.repeat(3) //’HoHoHo’

Returns an empty string if there is no parameter, or the parameter is 0. If the parameter is negative you’ll get a RangeError.

30
Q

What does this return?

replace(str1, str2)

A

METHOD

Find the first occurrence of str1 in the current string and replaces it with str2 (only the first!). Returns a new string.

“JavaScript”.replace(“Java”, “Type”) //’TypeScript’

You can pass a regular expression as the first argument:

“JavaScript”.replace(/Java/, “Type”) //’TypeScript’

replace( ) will only replace the first occurrence, unless you use a regex as the search string, and you specify the global (/g) option:

“JavaScript JavaX”.replace(/Java/g, “Type”) //’TypeScript TypeX’

The second parameter can be a function. This function will be invoked for every match found, with a number of arguments:

the string that matches the pattern
an integer that specifies the position within the string where the match occurred
the string

The return value of the function will replace the matched part of the string.

Example:

“JavaScript”.replace(/Java/, (match, index, originalString) => {
console.log(match, index, originalString)
return “Test”
}) //TestScript

This also works for regular strings, not just regexes:

“JavaScript”.replace(“Java”, (match, index, originalString) => {
console.log(match, index, originalString)
return “Test”
}) //TestScript

31
Q

What does this return?

search(str)

A

Return the position of the first occurrence of the string str in the current string.

It returns the index of the start of the occurrence, or -1 if no occurrence is found.

“JavaScript”.search(“Script”) //4
“JavaScript”.search(“TypeScript”) //-1

32
Q

What does this return?

slice(begin, end)

A

METHOD

The slice( ) method extracts a section of a string and returns it as a new string, without modifying the original string.

The second param (end) is optional.

“This is my car”.slice(5) //is my car
“This is my car”.slice(5, 10) //is my

if you set a negative first parameter, the start index starts from the end, and the second parameter must be negative as well, always counting from the end:

“This is my car”.slice(-6) //my car
“This is my car”.slice(-6, -4) //my

const str = ‘The quick brown fox jumps over the lazy dog.’;

console.log(str.slice(31));
// expected output: "the lazy dog."
console.log(str.slice(4, 19));
// expected output: "quick brown fox"
console.log(str.slice(-4));
// expected output: "dog."
console.log(str.slice(-9, -5));
// expected output: "lazy"
33
Q

What returns from this method?

split(separator)

A

METHOD

You can use the split() function when you want to turn a string into an array of substrings.

The parameter of the split( ) function is your separator. Each time the split( ) function finds the separator in the string, it starts a new array element.

The separator itself gets omitted and won’t be part of the array elements.

For example if you have a string “dog,cat,mouse” and you would like to have an array with the elements “dog”, “cat” and “mouse”, you could use split(“,”) for that:

const stringAnimals = "dog,cat,mouse"
const arrayAnimals = stringAnimals.split(",")
34
Q

What does this return?

startsWith(str)

A

METHODS

Check if a string starts with the value of the string str

You can call startsWith( ) on any string, provide a substring, and check if the result returns true or false:

“testing”.startsWith(“test”) //true
“going on testing”.startsWith(“test”) //false

This method accepts a second parameter, which lets you specify at which character you want to start checking:

“testing”.startsWith(“test”, 2) //false
“going on testing”.startsWith(“test”, 9) //true

35
Q

What does this method return?

substring( )

A

substring( ) returns a portion of a string and it’s similar to slice( ), with some key differences.

If any parameter is negative, it is converted to 0. If any parameter is higher than the string length, it is converted to the length of the string.

So:

“This is my car”.substring(5) //’is my car’
“This is my car”.substring(5, 10) //’is my’
“This is my car”.substring(5, 200) //’is my car’
“This is my car”.substring(-6) //’This is my car’
“This is my car”.substring(-6, 2) //’Th’
“This is my car”.substring(-6, 200) //’This is my car’

36
Q

What is returned when using this method?

toLowerCase( )

A

METHOD

Return a new string with the text all in lower case.

Same as toLocaleLowerCase( ), but does not consider locales at all.

“Testing”.toLowerCase( ) //’testing’

37
Q

What is returned while using this method?

toString( )

A

Returns the string representation of the current String object:

const str = new String("Test")
str.toString() //'Test'

Same as valueOf( ).

38
Q

What’s returned when using this method?

toUpperCase( )

A

METHOD

Return a new string with the text all in upper case.

Same as toLocaleUpperCase(), but does not consider locales at all.

“Testing”.toUpperCase( ) //’TESTING’

39
Q

What is returned from this method?

trim( )

A

Return a new string with removed white space from the beginning and the end of the original string

“Testing”.trim( ) //’Testing’
“ Testing”.trim( ) //’Testing’
“ Testing “.trim( ) //’Testing’
“Testing “.trim( ) //’Testing’

40
Q

What’s returned from this method?

trimEnd( )

A

Return a new string with removed white space from the end of the original string

“Testing”.trimEnd() //’Testing’
“ Testing”.trimEnd() //’ Testing’
“ Testing “.trimEnd() //’ Testing’
“Testing “.trimEnd() //’Testing’

trimRight() is an alias of this method.

41
Q
What is this?
const isReal = Boolean(true)
A

the Boolean( ) factory function

42
Q

What is this?

const isReal = true

A

The boolean literal syntax

43
Q

What are falsy values?

A

Falsy values, values interpreted as false, are

0 - 0
NaN
undefined
null
;("") //empty string

All the rest is considered a truthy value.