Javascript Data Types

In this article, we'll be learning about the Javascript data types.

ยท

4 min read

Hey Gang, welcome to this article on javascript data types, here we'll be looking at everything we need to know about the data types.

So let's get started and learn ๐Ÿš€

what is a data type?

Every variable has a value stored in it, the data type is nothing but the type of value stored in the variable. So data types are a classification which tells us what value we can add to a particular variable, and which further gives us clarification about the operations we can perform on that particular variable.

A value is always of a data type in javascript.

Before starting with data types, let's look at the typeof operator.

typeof operator

This operator is used to find the data type of a javascript variable.

// typeof operator
const x = 20
console.log(typeof x) //number

const y = "20"
console.log(typeof y) //string

Javascript data types can be classified into two basic types

  1. Primitive values
  2. Objects

Primitive values

Primitive values are the value which are immutable i.e values which can't be changed. These are the atomic pieces of data with no additional properties or methods.

Now let's look at the various primitive values in javascript.

1. undefined

It is a primitive data type which has only one value undefined. When a variable is declared but no value is assigned to that variable then it is considered to be of undefined data type by javascript.

let a
console.log(typeof a) //undefined

2. null

null is the intentional absence of the object value, we'll be learning about the objects later in this article.

// Null
let b = null
console.log(typeof b) //object

But now you'll be wondering why is typeof operator returning the object here since it is a null data type. Yes you're right it should return null and this is a bug in javascript. ๐Ÿฅฒ

3. number

This data type is used to represent both integer and fractional values(floating point numbers).

let a = 20
console.log(typeof a) //number

let b = 15.67
console.log(typeof b) //number

The range of number data type is from Number.MIN_VALUE to Number.MAX_VALUE.

console.log(Number.MAX_VALUE) //1.79769313e+308
console.log(Number.MIN_VALUE) //5e-324

4. BigInt

This is another numeric data type, it is used to store values with higher precision or values which are not in the range of the number data type. A BigInt value is created by appending n to the end of an integer.

// the "n" at the end means it's a BigInt
let a = 120000000n
console.log(typeof a) //bigint

5. Boolean

The boolean data type is used to represent logical values, it has two values true and false. This type is generally used to store yes or no values.

let a = true
console.log(typeof a) //boolean

let b = false
console.log(typeof b) //boolean

const c = 20 < 30
console.log(typeof c) //boolean

6. String

String are textual values, it is a sequence of one or more characters.

Strings in javascript must be surrounded by the quotes and 3 types of quotes are supported.

  1. Double quotes: "Hello world".
  2. Single quotes: 'Hello world'.
  3. Backticks: `Hello world`.
// Double quotes
let a = "Hello world"
console.log(typeof a) //string

// Single quotes
let b = 'Hello world'
console.log(typeof b) //string

// Backticks
let c = `Hello world`
console.log(typeof c) //string

7. Symbol

A symbol represents a unique identifier. A value of this type can be created using Symbol():

let a = Symbol("Hello")
console.log(typeof a) //symbol

Objects

Objects are data types with key-value pairs, these are written with curly braces. The object data type is a special type of data type because all other primitive data types can only contain single values, but objects can contain one or more values.

let a = {
  name: "Jonn Snow",
  age: 30,
}
console.log(typeof a) //object

Thank you for reading, I'll appreciate your feedback and doubts in the comments section if there are any.
Happy Coding ๐Ÿš€

Wishing you all the best for further learning!

Did you find this article valuable?

Support Kunaaal's blog by becoming a sponsor. Any amount is appreciated!

ย