Chapter 2 - Data Types

Chapter 2 - Data Types

ยท

3 min read

Hello Readers ๐Ÿ‘‹...

I hope you are doing good and safe.

This article in the JavaScript for beginners series is all about Data Types. While JavaScript is dynamically typed and you don't need to specify the types it's still important to know them and know what they are used for.

This is also useful if you plan on working with Typescript! So without further delay, Let's get started.

Dynamic Types

A value in JavaScript is always of a certain type. Since it is a dynamically typed language the types are based on the value of the variable is assigned. It also means that a variable can change type if the value changes.

let message = "Hello Readers";   // string
message = 123456;   // number

Number & Big Int

The number type represents both integer and floating-point numbers.

let number = 659;
let float =3.45;

Due to a technical limitation, the Number type cannot hold a value larger than (2^53 - 1) which is 900719925470991. For those numbers, you need to use the BigInt type which is created by adding an n to the end of the number.

let bigInt = 123456789012345678901901234890n;

String

The string type is used for any text variables. You can use double or single quotes to create a string variable.

let str = "Double Quotes";
let str2 = 'Single quotes work too';

Backticks are "extended functionality" quotes. They allow you to embed another variable into the string.

let phrase = `Backticks to embed strings ${str}`;

Boolean

Then boolean type has only two values: true and false. It's commonly used to store yes / No values.

let isActiveUser = true;
let isLoggedIn = false;

They can also be used to store a result of a comparison.

let isGreater = 7 > 3;  // true
let isLess = 9 < 3;   // false

Array

The array type holds a list of values. Those values can be of any one type (i.e. all values need to be the same type).

const rainbowColours = [
"Red",
"Orange",
"Yellow",
"Green",
"Blue",
"Purple"
]

You can access values from an array using the index (which starts at 0 for the first value)

let favouriteColour = rainbowColours[5];

Object

The object type can hold key/value pairs. It's like having a big group of variables that are related. Unlike an array, the key/value pairs can be of different types.

const user = {
name: "Harshada",
posts: 2,
isLoggedIn: false
};

You can access values from an object by calling the object then the key you want.

let welcomeMessage = `Hello ${user.name}`;

Undefined / Null

A variable without a value will be undefined. Although you can also explicitly set a variable to be undefined - It's not recommended!

let newVariable;  //undefined
let undefinedVariable = undefined;

If you have a variable where the value is currently empty or unknown you can use the special null value.

let age = null;
newVariable = null;

Summary

  • Number is used for integer and float values

  • Large numbers use the BigInt type and have an n at the end.

    • A string is used for text and has other variables embedded within it.

    • Use a boolean for true/false values.

    • An array can hold a list of values of the same type.

    • And an object has key/value pairs and is like a collection of related variables.

Hope you found this useful, let me know your thoughts in the comments. Thank You for reading...

ย