Sitemap
Dinusha Somarathne

Dinusha Somarathne

JavaScript — Primitive data types

3 min readJun 14, 2023

--

JavaScript is a loosely types language. Therefore you don’t need to specify what type of information will be stored in a variable in advance. for example, if you declared a value of “Singapore”, JavaScript automatically defines it as a string type.

JavaScript has seven primitive data types — (Primitive Data type is predefined by the language and named by a reserved keyword.)
1. String
2. Number
3. Boolean
4. Null
5. Undefined
6. Symbol
7. BigInt

String

The string can include any number, letter, or symbol. There are different ways to declare a string.

  1. Double quote
let doubleQuote = "Learning JS";
console.log(doubleQuote);

2. Single quotes

let singleQuote = 'Learning JS';
console.log(singleQuote);

3. Backticks — a special template string in which you can use variables directly.

let name = "Thomas";
let message = `Hello ${name}`;
console.log(message);

Escape characters
The backslash can be used to ensure the interpreter doesn’t see the single or double quotes marks and end either string too early

let message = 'What\'s your favuourite colout? Is it "Red"?';
console.log(message);

Also, you can use an escape character to create a linebreak with \n

let message = 'What\'s your favorite colour? \n Is it "Red"?';
console.log(message);

Number

number type, including floats and decimals. Numbers are not wrapped in quotes.

const age = 28;
const temperature = 84.8

Boolean

The Boolean data type can hold two values. true and false.

let lightIsOn = false;

Null

null is a special value for saying that a variable is empty or has an unknown value. It should use lowercase for null. It is better to assign null to a variable when you want to say it is empty and unknown at first.

let firstName = "Thomas";
let lastName = null;
console.log(firstName + " " + lastName);

Undefined

The variable that has not been assigned a value.

let firstName = "Thomas";
let lastName;
console.log(firstName + " " + lastName);

We can also purposefully assign an undefined value. Manually assigning undefined is a bad practice.

Symbol

A “symbol” represents a unique identifier. The symbol is a brand new data type introduced in ES6.

Get Dinusha Somarathne’s stories in your inbox

Join Medium for free to get updates from this writer.

In this example, JavaScript concludes that strings are the same. They have the same value and the same types.

Press enter or click to view image in full size

In this example each symbol is unique. They contain the same string but they are not the same, and output false when compared.

Press enter or click to view image in full size

BigInt

It represents integers that are out of the range of the number types

const n = 8007199254740991n;

You can only operate on BigInt with other BigInt.

Press enter or click to view image in full size

typeof Operator

It is use to check the type of a variable

Press enter or click to view image in full size

--

--