DEV Community

Ayobami Ogundiran
Ayobami Ogundiran

Posted on • Updated on

Primitive Data Types in JavaScript.

Welcome to this lesson, in this lesson, we will talk about the primitive data type in JavaScript.

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.

String

let name = 'Ayobami';
let school = "Harvard";
let greeting = 'Hello World! I am becoming a developer';
Enter fullscreen mode Exit fullscreen mode

The values of the above variables are strings because they are texts and they are wrapped with a single or double quote. It is common to use a single quote in JavaScript but both of them are valid.

Let's check the type of the variable school:

console.log(typeof school); // Viola, it shows string in the console. 
Enter fullscreen mode Exit fullscreen mode

Number

"Number" is another primitive data type in JavaScript. Let's use it.

let age = 20;
let quantity = 7;
Enter fullscreen mode Exit fullscreen mode

We can use them for arithmetic calculations. That is, we can use number with +, -, /, %, * as in:

let quantity = 7 * 4; // console.log(quantity);
Enter fullscreen mode Exit fullscreen mode

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); 
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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.

console.log(typeof quantity); 
Enter fullscreen mode Exit fullscreen mode

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

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);
Enter fullscreen mode Exit fullscreen mode

let's check the type of isMarried.

console.log(typeof isMarried) // that is boolean.
Enter fullscreen mode Exit fullscreen mode

It is time for undefined;

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:

let school;
Enter fullscreen mode Exit fullscreen mode

its value and type will both be undefined.

let's check that by doing

console.log(school);// undefined
console.log(typeof school);// undefined
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

undefined is logged for both of them in the console.

Finally, let's talk about null.

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.
Enter fullscreen mode Exit fullscreen mode

let check the type of address.

console.log(typeof address)// object.
Enter fullscreen mode Exit fullscreen mode

Oooops!

Why object? Shouldn't that be null? That is how it is in JavaScript. Null is a special case of the primitive data type.

Now, you have a good understanding of primitive data type in JavaScript.

Stay connected.

See you in the next lesson?

One more thing

Are you struggling to understand and build projects with JavaScript?

Don't worry!

I will teach you JavaScript and Project Making Fundamentals with simple illustrations and examples that make everything so easy.

You can now handle any difficult projects without fear.

Don't believe me, get a preview from the link below to see for yourself:

👉 https://bit.ly/3o3TMyg

Top comments (0)