Primitives Flashcards

0
Q

Primitives:

What are the 5 types of primitives in JavaScript?

A

Boolean, number, string and undefined.

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

Primitives:

How do you test if a variable is a number or not?

A

isNan()

isNaN(“9”) //true
isNaN(9) //false

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

Primitives:

Which primitive type does the “typeof” operator surprisingly declare as an object even though it is not.

A

Null.

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

Primitives:

s1 = "Hello";
s2 = new String("Hello");

What is the difference between s1 and s2?

A

s1 is a primitive and s2 is an object.

typeof s1; //string
typeof s2; //object

In most cases it is preferable to use the primitive instead of the object wrapper.

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

Primitives:

What will

“primitive”.someProperty = 99;
return “primitive”.someProperty;

return?

A

Undefined.

You can not set a property on a primitive.

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

Primitives:
True or false

s1 = new String("Hello");
s2 = new String("Hello");
s1 == s2
A

False.

Because s1 and s2 were created using object wrapper, they are objects rather primitive values. And by definition and object can only be equal to itself. So both s1== s2 and s1 === s2 are false.

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

Primitives:

Strings in JavaScript are made of which type of Unicode encoding UTF-8, UTF-16 or UTF-32?

A

UTF-16

This is important because any character that is not in the basic multilingual plane is represents by a surrogate pair (2 units) which affects things like length and charAt…

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

Primitives:

How do you displays a number as a string with 2 fixed decimal places?

A

n.toFixed(2)

If the number has more than 2 decimal places it will be rounded.

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

Primitives:

How do you displays a number as a string in exponential notation?

A
var n=123456.789
n.toExponential(3) //1.235e+5

There will always be one digit before the decimal point and the specified number of digits after.

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

Primitives:

When converting a string to a number JavaScript will try to convert the string to the number in context (“4” * “5” = 20)

What methods can you use to force the conversion?

A

parseInt() and parseFloat()

If they cannot convert the string to a number they will return NaN.

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

Primitives:

What will parseInt(“4 blue cars”) return?

A

4

parseInt() and parseFloat() will always attempt to return a value if the string begins with a number.

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

Primitives:

How do you display a number in a format that is octagonal?

A
var n=17;
octagonal = n.toString(8); // 21

The optional argument specifies the radix between 2 & 32.

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

Primitives:

What is a shorthand for converting the value of x to a Boolean?

A

!!x

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

Primitives:

Does null == undefined?

A

Yes, and both types are falsy.

But null !== undefined.

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

Primitives:

What does it mean that strings are immutable in JavaScript?

A

Methods preformed on a string never alter the original string they only return a new one.

var a=”bunny”;
var b=a.toUppercase();
console.log(a); //bunny
console.log(b); //Bunny

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

Primitives:

Given that Array(3, 4, 5) creates an array with a length of 3 [3,4,5]. What does Array(5) create?

A

An array of 5 undefined elements.

16
Q

Primitives:

What type does String.charAt() return?

A

A string with a length of 1.

JavaScript has no concept of the type character.