6/30 Explanation


#1

Hello all so i completed this exercise with help (thanks forums) but i was wondering if someone could explain a bit more what typeof is and why we would ever use it in javascipt thanks in advance :slight_smile:


#2

'typeof' is a JavaScript operator that returns the datatype of an object. It has limitations, but works well with all primitives.

console.log(typeof "");
console.log(typeof Math.PI);
console.log(typeof true);
console.log(null);
console.log(undefined);

When used on reference objects it can be misleading.

console.log(typeof function(){});
console.log(typeof {});
console.log(typeof []);

Output

string
number
boolean
null
undefined
function
object
object

Notice the last one? Arrays are objects with a special class.

Primitives are not objects, but are detected and given a wrapper object that has the best fit with the data. Numbers get a Number wrapper, strings get a String wrapper, Booleans get a Boolean wrapper, null gets an Object wrapper.

.


#3

@ginganinja808,
And with the explanation of @mtf
you can see the structure ??

An object has one or more properties, separated by a comma_,

A property has a property-key and it's associated VALUE

The associated VALUE can be anything from:

•Number
•String
•Boolean
•Symbol
•Object  ◦Function/'Method'
         â—¦Array
         â—¦Date
         â—¦RegExp
         â—¦Math
•Null
•Undefined