Primitives Flashcards
Primitives:
What are the 5 types of primitives in JavaScript?
Boolean, number, string and undefined.
Primitives:
How do you test if a variable is a number or not?
isNan()
isNaN(“9”) //true
isNaN(9) //false
Primitives:
Which primitive type does the “typeof” operator surprisingly declare as an object even though it is not.
Null.
Primitives:
s1 = "Hello"; s2 = new String("Hello");
What is the difference between s1 and s2?
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.
Primitives:
What will
“primitive”.someProperty = 99;
return “primitive”.someProperty;
return?
Undefined.
You can not set a property on a primitive.
Primitives:
True or false
s1 = new String("Hello"); s2 = new String("Hello"); s1 == s2
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.
Primitives:
Strings in JavaScript are made of which type of Unicode encoding UTF-8, UTF-16 or UTF-32?
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…
Primitives:
How do you displays a number as a string with 2 fixed decimal places?
n.toFixed(2)
If the number has more than 2 decimal places it will be rounded.
Primitives:
How do you displays a number as a string in exponential notation?
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.
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?
parseInt() and parseFloat()
If they cannot convert the string to a number they will return NaN.
Primitives:
What will parseInt(“4 blue cars”) return?
4
parseInt() and parseFloat() will always attempt to return a value if the string begins with a number.
Primitives:
How do you display a number in a format that is octagonal?
var n=17; octagonal = n.toString(8); // 21
The optional argument specifies the radix between 2 & 32.
Primitives:
What is a shorthand for converting the value of x to a Boolean?
!!x
Primitives:
Does null == undefined?
Yes, and both types are falsy.
But null !== undefined.
Primitives:
What does it mean that strings are immutable in JavaScript?
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