What is a primitive data type?
A primitive data type refers to the smallest unit of data, that is, it has no sub-division. A primitive data type could also be called non-derived data type.
It is any data that is not an object or data that doesn't have its own specific methods.
The primitive data types are basically six in number but if null is included, they are seven in number.
These are the primitive data types or value types.
We have "String", "Number", "Undefined", "Null", "Boolean", "BigInt" and "Symbol". Here, we only talk about four of them because bigint and symbol are a bit advanced for a beginner.
Now, let's talk about String.
let name = 'Ayobami'; let school = "Harvard"; let greeting = 'Hello World! I am becoming a developer';
Let's check the type of the variable school:
console.log(typeof school); // Viola, it shows string in the console.
let age = 20; let quantity = 7;
We can use them for arithmetic calculations. That is, we can use number with +, -, /, %, * as in:
let quantity = 7 * 4; // console.log(quantity);
you should see 28 in the console.
Do you see that?
let price = 20 + 40; console.log(price);// Yeah! there is 60 in the console. let remainder = 21 % 4; // console.log(remainder);
One is the remainder after dividing 21 by 4.
Now, let's write variable named "quantity" and check its type in the console.
let quantity = 20;
Don't forget this will throw an error because we cannot re-declare a let variable as we have written earlier.
Okay, let's confirm it.
Oops! Do you see that? It throws an error.
Let's just delete one and do console.log(typeof quantity) again.
Gon gon! Its type is number.
And bla bla bla blabla. Let's continue with Boolean.
Boolean is used to express true or false value. So, Boolean is used to indicate whether something is true or false as in:
let isYoung = true;// yeah! I am young. let isMarried = false; Nay! I am single and searching. let isInteresting = true; My lessons are always on fire (on point);
let's check the type of isMarried.
console.log(typeof isMarried) // that is boolean.
It is time for undefined;
If you remember, I said in the last lesson that a variable as two parts, name and value, right?
If you only declare its name as in:
its value and type will both be undefined.
let's check that by doing
console.log(school);// undefined console.log(typeof school);// undefined
Dundrum! You see there is undefined in the console.
Even if you set the value of a variable to undefined, its type and value will both be undefined.
let name = undefined; console.log(name); // undefined console.log(typeof name) // undefined
undefined is logged for both of them in the console.
Finally, let's talk about null.
Null is considered to be a primitive data type. And it means nothing. No data;
Let's use it.
let address = null; console.log(address)// boom! null.
let check the type of address.
console.log(typeof address)// object.
See you in the next lesson?
You can now handle any difficult projects without fear.
Don't believe me, get a preview from the link below to see for yourself: