DEV Community

Danish Saleem
Danish Saleem

Posted on

JavaScript for Beginners - Chapter 2: Data Types

Chapter two in the JS 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.

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 the variable is assigned. It also means that a variable can change type of the value changes.

let message = "Hello"; // string
message = 12345; // number
Enter fullscreen mode Exit fullscreen mode

Number and BigInt

let number = 123;
let float = 0.12;
Enter fullscreen mode Exit fullscreen mode

Due to a technical limitation, the Number type cannot hold a value larger than (2^53-1) which is 9007199254740991. 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 = 123456789012345678901234567890n;
Enter fullscreen mode Exit fullscreen mode

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 works too!";
Enter fullscreen mode Exit fullscreen mode

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

let phrase = `Backtick to embed strings ${str}`;
Enter fullscreen mode Exit fullscreen mode

Boolean

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

let isFieldChecked = true;
let isLoggedIn = false;
Enter fullscreen mode Exit fullscreen mode

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

let isGreater = 4 > 2; // true
let isLess = 5 < 1; // false
Enter fullscreen mode Exit fullscreen mode

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 rainbowColor = ["Red", "orange", "Yellow", "Green", "Blue", "Purple"];
Enter fullscreen mode Exit fullscreen mode

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

let favouritColour = rainbowColour[5];
Enter fullscreen mode Exit fullscreen mode

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 paris can be of different types.

const user = {
    name: "Dev",
    role: "admin",
    isLoggedIn = false
  };
Enter fullscreen mode Exit fullscreen mode

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

let welcomeMessage = `Hello ${user.name}`;
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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

let age = null;
newVariable = null;
Enter fullscreen mode Exit fullscreen mode

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 have other variables embedded with 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.

Let's connect 💜

You can follow me on Twitter, Instagram & GitHub

If you like this post. Kindly support me by Buying Me a Coffee

Buy Me a Coffee

Discussion (0)