DEV Community

Codecupdev
Codecupdev

Posted on

Primitive Data Types in JavaScript

What is a primitive type?

Data can come in all shapes and sizes. We use the term data type to specify the type of a specific value.

In our everyday language use, we differentiate between letters and numbers and usually, we will use them for different purposes. In programming, it is no different. We call the most basic data types primitives.

The main features of a primitive data type are:

  • It is not an object (a collection of keys and values)

  • It does not have its own methods (property of an object which is a function)

  • It is immutable (it cannot be changed and is read-only).

The primitive data types in JavaScript

The primitive data types in JavaScript are:

  • Number
  • Boolean
  • String
  • Null
  • Undefined
  • Symbol
  • BigInt Let's briefly take a look at each of these types!

Number

In JavaScript, there is only one type of number and this can be any numerical value with or without decimals. We can use whole numbers, positive numbers and negative numbers. We can also use decimals (also known as floating-point values).

50
//Whole number
1
//Positive number
-1
//Negative number
0.1
//Decimal
Enter fullscreen mode Exit fullscreen mode

Boolean

One of two values true or false (Not wrapped in quotes). A bit like saying yes or no.

true
false
Enter fullscreen mode Exit fullscreen mode

String

The string data type refers to any set of characters wrapped inside single or double-quotes. The following would both be examples of strings:

"I am a string";
'I am a string';
Enter fullscreen mode Exit fullscreen mode

Null

Represents nothing or a nonvalue but often this is intentionally set. You could create a variable and intentionally set the value of the variable to null. For example:
let myEmptyVariable = null;

Undefined

Undefined also represents no value. As opposed to null, variables that are defined with no value or variables which have not been declared (created at all) will be given the value of undefined.

let a;
console.log(a);
// ---> Returns undefined
Enter fullscreen mode Exit fullscreen mode

Undefined is often given to us in the form of an error. Functions will return undefined if no return value is set.

Symbol

Symbols were introduced in the ES6 version of JavaScript. They are unique identifiers that cannot be mutated and can be created using the function Symbol().

const firstSymbol = Symbol();
const secondSymbol = Symbol();
console.log(firstSymbol === secondSymbol);
// ---> Returns false
Enter fullscreen mode Exit fullscreen mode

BigInt

Useful for representing large numerical values, Specifically those over 15 digits long. You can create this by placing n at the end of a literal number. It can also be created by using the BigInt constructor and passing the value into the parameters.

const ourInteger = 1118998854361892n;
const ourSecondInteger = BigInt(1118998854361892);
console.log(ourSecondInteger);
// ---> Returns 1118998854361892n

Enter fullscreen mode Exit fullscreen mode

Please feel free to post any comments, questions or feedback!

Top comments (0)