General Primitive Knowledge Flashcards
typeof 23.5;
typeof ‘Call me Ishmael.’;
typeof false;
typeof 0;
typeof [ ]
typeof { }
typeof null;
typeof undefined;
typeof Infinity
number
string
boolean
number
object
object
object
undefined
number
Why does typeof null return “object”
Basically a legacy mistake.
In the first implementation of JavaScript, JavaScript values were represented as a type tag and a value.
The type tag for objects was 0.nullwas represented as the NULL pointer (0x00 in most platforms).
Consequently, null had 0 as type tag, hence the “object”typeofreturn value.
Name all the data types
How many are there?
8
Number
Boolean
String
Symbol
BigInt
null
undefined
Object
What are the data type categories?
How do the data types fit into them?
Primitive and composite Primitive data types 1 Number 2 String 3 Boolean
4Symbol
5 BigInt
6 Null
7 undefined
Everything else is an object or Composite data type
8 Object
Array
Functions
What makes a composite data type?
What makes a primitive?
Composite typesare made up of heterogeneous data as one unit.
A composite type can contain not only strings, numbers, Booleans,undefinedvalues, and null values, but even other composite types.
JavaScript supports three composite types: objects, arrays, and functions.
A primitive in JS is defined as NOT an object
When a primitive value is assigned to a variable (eg let foo = ‘bar’), the variable is set to that value directly. An object variable value is set to the destination in memory where the object data is stored.
Explicitly change data
- return argument as a number (2 main ways)?
- return argument as a string, 2 ways
- return an integer if it’s in the argument
- return a float if it’s in the argument
Number(‘424’), +’1’
String(4124),
Object.prototype.toString(number base)
ex (12312).toString(10);
Number.parseInt( )
Number.parseFloat( )
parseInt and parseFloat find the first number possible:Number.parseFloat(‘11.11fda2 1’ //returns 11.11
But they will return NaN if a non number comes first::Number.parseFloat(‘a11.11’) //returns NaN
Where are primitive and reference values stored?
Primitive values are data that are stored on the stack. Primitive value is stored directly in the location that the variable accesses.
Reference values are objects that are stored in the heap.
Can you change a primitive?
No, they can be created and destroyed but not changed like objects
(which means there are no mutating methods for strings, numbers, and boolean.
Check if something is specifically NaN
Number.isNaN()
or
let isNaN = function(x) {
return !(x===x);
}
(NaN is the only thing that is not equivalent with itself)
Write a function that checks whether the input is specifically NaN
NaN is the only thing that does not equal itself function isItNaN(input) { if (input !== input) { console.log(true); } else { console.log(false); } }
What happens:
let b = (a = 1);
console. log(b);
console. log(a);
Why? (Incomplete)
evaluates to:
1
1
The a variable is declared n.
Why doesn’t NaN === NaN evaluate to true?
NaN is designed to propagate through all calculations, infecting them like a virus, so if somewhere in your deep, complex calculations you hit upon a NaN, you don’t bubble out a seemingly sensible answer.
null == undefined
true
but null === undefined evaluates to false
When using > < >= and <=
What happens with null and undefined?
they are converted to numbers
(same as +null or Number(null) etc.)
(null to 0, undefined to NaN)
alert( undefined > 0 );
alert( undefined < 0 );
alert( undefined == 0 );
// false (1)
// false (2)
// false (3)
- undefined is converted to NaN