JavaScript Data Types with Examples
Five most common JavaScript Primitive Data Types
What is JavaScript Data Types?
Generally, in programming, data types is an essential topic.
It is important to know what type of data you are working with to be able to carry out tasks successfully.
Simply put, without data types, a computer will find it difficult to execute commands.
It’s crucial that we learn each of these data types, otherwise data can get stored in an inappropriate way which will eventually leads to issues in your code execution.
Data types in JavaScript describe the different types or kinds of data that you will be working with and storing in variables.
JavaScript is a loosely typed or a dynamic language. Variables in JavaScript are not directly associated with any particular value type, and any variable can be assigned.
JavaScript Data Types can be classified into two sets, namely:**
- Primitive Data Types
- Reference Data Types
However, in this article, we are going to focus on the Primitive Data Types.
The five most basic data types in JavaScript are;
- String
- Number
- Boolean
- Null
- Undefined
We will learn about each of them in detail with examples and how to implement them in your code.
I. String
A string is a collection of alphanumeric characters, a string is any series of characters that are interpreted literally by a script. E.g., "Hello World" is example of a string - A string is attached to a variable as displayed in the example below.
// this is a string
let str = "Welcome"
A string in JavaScript must be surrounded by quotes.
There are 3 types of quotes in JavaScript.
// 1. Double quotes
let greeting = "Hello"
// 2. Single quotes
let platform = 'Hashnode'
// 3. Backticks
let sentence = `${greeting}, this is my first blog post on ${platform}!`
// Code Output: Hello, this is my first blog post on Hashnode!
There is nothing wrong in using any of the quotes when assigning string to a variable, however, the backticks come with extra functionality, they allow us to embed variables and expressions into a string by putting them in ${…} as shown above.
II. Number
JavaScript has only one type of numbers.
The number type represents both integer and floating point number.
// this is a number
let num = 123 // Without decimals
num = 123.90 // With decimals
Just like in Mathematics, there are many operations for numbers, e.g. multiplication *
, division /
, addition +
, subtraction -
etc - You can easily perform simple arithmetic calculation in JavaScript, for example;
let num1 = 10;
let num2 = 15 ;
let total = num1 + num2; // Code Output: 25
III. Boolean
The Boolean type has two values: true
and false
- Commonly used in logical operations.
To store Booleans; true/false values true
means “yes, correct”, and false
means “no, incorrect”.
Example 1:
let isLessThan = 100 < 30; // Code Output: false;
// 30 is less than 100
Example 2:
let n1 = 10;
let n2 = 10;
let n3 = 20;
(n1 == n2) // Returns true
(n1 == n3) // Returns false
IV. Null
In computer science, a null value represents a reference that points to a nonexistent or invalid.
The value null represents the intentional absence of any object value.
A variable that is set to null is declared as intentionally nothing/empty.
let userName = null; // Returns null
In a nutshell, Null is a special value which means "void", “empty” or “unknown”.
V. Undefined
The global undefined property represents the primitive value undefined .
A variable that has been declared but NOT assigned to a value is of type undefined.
The special value undefined is similar to Null but a data type of its own entirely.
let userName; // Returns undefined
Or
let age = undefined; // Returns undefined
Highly not recommended, unless you really know what you are doing.
Bonus
IV. *The typeof Operator
The typeof in JavaScript is an operator used for checking a data type. The operand can be any variable, function, or object whose type you want to find out using the typeof operator.
A call to typeof simply returns the data type pass to it:
typeof undefined // Returns "undefined"
typeof 100 // Returns "number"
typeof true // Returns "boolean"
typeof "foo" // Returns "string"
Conclusion
These are five most basic Primitive Data Types in JavaScript. We shall be learning about Reference Data Types in the coming days.
Thanks for reading - If you have any questions/suggestions, kindly leave them in the comments section below, and I'll be glad to answer every single one.