Mid Term study Javascript types Flashcards
The two types of variables and when to use them?
let and const
let is used for variables whose value changes during runtime
const is used for variables whose value never changes during runtime
What are the 6 Primitive Types in Javascript
Number, BigInt, String, Boolean, null(empty), undefined(value not assigned)
What are the non-Primitive types in Javascript?
Objects: coded using {}
Symbols
How do parseInt() and parseFloat() work in javascript?
both read digits from a string until they can’t read any more digits
Example:
parseInt(“16px”) will stop before p
How to
1.Convert String to numbers
2.Convert things to String
1.parseInt(), parseFloat(), Number()
2.String()
What does this output?
console.log(“5” / “2”);
console.log(5 / 2);
console.log(“5” / 2);
2.5
2.5
2.5
Javascript does implicit conversion in this case
How to:
Convert to Booleans
Boolean() or !!thingtoconvert
example: console.log(!!n);
What is the output?
console.log(Number(undefined));
console.log(Number(true));
console.log(Number(false));
console.log(Number(null));
console.log(Number(“ 1234abc”));
console.log(Number(“ “));
console.log(Number(undefined)); = NaN
console.log(Number(true)); = 1
console.log(Number(false)); = 0
console.log(Number(null)); = 0
console.log(Number(“ 1234abc”)); = NaN
console.log(Number(“ “)); = 0
What is the output?
console.log(Boolean(0));
console.log(Boolean(1));
console.log(Boolean(null));
console.log(Boolean(undefined));
console.log(Boolean(NaN));
console.log(Boolean(“foo”));
console.log(Boolean(25));
console.log(Boolean(0)); = false
console.log(Boolean(1)); = true
console.log(Boolean(null)); = false
console.log(Boolean(undefined)); = false
console.log(Boolean(NaN)); = false
console.log(Boolean(“foo”)); = true
console.log(Boolean(25)); = true
non 0 values will be true
0 and error types will be false