Primitives and Objects
There are two types of data in JavaScript:
- Primitives
- Objects
Primitives
String
A string must be surrounded by quotes. There are three types of quotes:
//singlequote
constfirstName='Davina';
//doublequote
constmiddleName="Holderness";
//backticks
constsurName=`Ashton`;
Each of the above are valid strings in JavaScript.
Number
An integer is a whole number.
A float is a number with a decimal.
In JavaScript, the type 'number' represents both integers and floats
constage=10;
console.log(typeofage);
//number
constheight=5.5;
console.log(typeofheight);
//number
Boolean
A boolean value can only be true or false.
constitIsSunnyInEngland=false;
console.log(typeofitIsSunnyInEngland);
//boolean
consthummusIsDelicious=true;
console.log(typeofhummusIsDelicious);
//boolean
Null
Null means it is empty or the value is unknown. Strangely, the type of null is an object in JavaScript.
constitIsNull=null;
console.log(typeofitIsNull);
//object
Undefined
Undefined means the value is not set.
During the creation phase of the global execution context, all variables initially have an undefined value.
You should avoid assigning undefined to variables, but instead use null.
We use undefined to check if a variable has been declared and if a value is assigned to it.
console.log(age)
//UncaughtReferenceError:ageisnotdefined
We see here that undefined means the variable is declared, but a value has not been assigned to it:
varage;
console.log(age)
//undefined
It is only when a value is assigned to it, that it stops being undefined :
varage=21;
console.log(age)
//21
Symbol
For a detailed explanation:
https://www.keithcirkel.co.uk/metaprogramming-in-es6-symbols/Objects
Objects are a collection of key:value pairs. The key is a string, the value can be anything.
constowner={
name:'Toby',
}
constcat={
color:'ginger',
type:'Tabby',
paws:4,
owner:owner,
lives:[1,2,3,4,5,6,7,8,9],
scratchMe:function(){
console.log('ouch!');
}
}
console.log(typeofcat);
//object
console.log(cat.color);
//ginger;
console.log(cat.owner);
//{name:'Toby'}
console.log(cat.scratchMe);
//[Function:scratchMe]
//Ifafunctionisassignedasthevalueofanobject,youcancallitlikeso:
cat.scratchMe();
//ouch!
Arrays are objects within JavaScript
Objects have methods and properties.
constfavouriteFoods=['Jollof','Ravioli','Hummus'];
console.log(typeoffavouriteFoods);
//object
console.log(favouriteFoods.length);
//3
We know an array is an object because it has values such as Array.length
They also have methods such as Array.forEach(), Array.map(), Array.reduce(), - these are known as array iteration methods.
functions
In JavaScript, any data type which isn't a primitive is an object. This means that functions are also objects.
How is this possible?
//functiondeclaration
constmakeMe=function(){
console.log('makeme');
}
console.log(typeofmakeMe);
//function
console.log(typeofmakeMe.prototype);
//object
//arrowfunction
constdareMe=()=>{
console.log('dareme');
}
console.log(typeofdareMe);
//function
console.log(typeofdareMe.prototype);
//undefined
With our simple test above, we can see that the prototype of our function is actually an object.
We will find out about prototypes in the next section. For now, you just need to know that functions are objects, because functions inherit from the object prototype.