Data types in JavaScript

Data types in JavaScript

Beginner’s guide to data types in JavaScript.

Hello! everyone 👋🏾.

Welcome, in this post we’ll be learning about data types in JavaScript.

introduction

What is a data type?

In JavaScript the values/data that can be stored in a variable are grouped into different categories based on the kind(type) of data they are. The six basic groups of data are:

  • Numbers
  • Strings
  • Booleans
  • Undefined
  • Null
  • Arrays
  • Objects

There are two more (BigInt type and Symbols) but they fall outside the scope of this post.

When asked of the data type of a variable/value you’re simply being asked what kind of data is stored in the variable/which group the data belongs to. For example
what is the data type of years?

let years = 7456;

Answer: Number.

Describing the various data types

The following paragraphs would describe briefly the various kinds of data (data type) in JavaScript and help you spot them when you come across them.

Number type

Numbers are data represented in a numeric format. ie 76. Numbers can be can be written as positive or negative integers, decimals or exponentials. Decimals are used in writing numbers but not commas. For example

let age = 76;
let averageScore = 12.4;
let GPA = 0.8;    :)
let resultantForce = 17e5;

String type

Strings are data represented in a textual or alphanumeric format. Strings are enclosed in either single quotes, double quotes or backtick characters. Strings can include any character at all (numbers, alphabets, special characters etc). The only caution is, when including quotes as part of your String characters make sure they don’t match the ones surrounding the string. For example

let carBrand = “Mercedes”;
let article =‘he said that,“everyone must be careful” ‘;

When a number is enclosed in either single or double quotes, it is treated as a string For example

let age = “76”;
// age is NOT a number but a string.

Boolean type

Boolean refers to a value that is set to take only one of two forms. Either a simple true or false, 0 or 1 or any two distinct values(ie 55 and 67). Booleans values are often used for conditional testing For example

let beautiful = true;
if (beautiful === true) {
    console.log(“wow!!”)
}

Undefined type

When a variable is declared without assigning any value to it, JavaScript by default assigns it an undefined type which means the value is not defined. For example

let age;

Null type

Null value is a value that represents emptiness/nothing. The null value must deliberately assigned. For example

let age = null;

NB one difference between the undefined type and the null type is that undefined means the value is not defined while the null means there is nothing in the variable.

Object

An object is a collection values that are uniquely identified(named). The values are represented as name: value / key: value pairs( name is alternatively called key). The value of the pair can be any of the data types mentioned in this article or a function. The pair is called a property if the value is one of any of the data types mentioned and called method if the value is a function. For example

const car = {
           brand:”Mercedes”,
           price:986000
           buy: function (brand, price) {
                         console.log(`the ${brand} car costs ${price}` } 
          }

In this example “ brand” and “ price” are all “keys” and “ Mercedes” and 986000 are values of these keys respectively. The “buy” is a method of the object.

Arrays

An array is an ordered collection of data. Data stored in arrays are indexed beginning from 0. Arrays are written with square brackets and each array item is separated by a comma. Any of the data types can be stored in an arrayFor example

// an array of numbers 
const carBrands = [54,67,88,99,456];

// an array of strings 
const carBrands = [“Mercedes”, “Toyota”,”Honda”,”Range Rover”];

// an array of objects  
const carBrands = [ 
         {
           brand:”Mercedes”,
           price:986000
         },
         {     
           brand:”Honda”,
           price:457000
          }
] ;

// an array of arrays 
const carBrands = [ 
[“Mercedes”, “Toyota”,”Honda”], 
[54,67,88,99,456]
] ;

How to check the data type of a variable or value

To check the data type of a variable or value, the typeOf operator is used. For example

let age = 76;
typeOf(age) //-> Number 

let carBrand = “Mercedes”;
typeOf(carBrand) //-> String 

let beautiful = false;
typeOf(beautiful) //-> Boolean

let name;
typeOf(name) //-> undefined

const carBrands = [Mercedes”;
typeOf(carBrands) //-> Array

NB All JavaScript data types are checked using the typeOf operator except the null type. To check if a variable is null, use the tripple equality operator (===). For example

let carGarage = null;
typeOf(carGarage)
//-> object 

console.log(carGarage === null);
//-> true

Conclusion

All the data types mentioned above are further divided into two groups (primitive types, reference types) which will be discussed in another post. Also, the above mentioned data types have only been discussed briefly for the purpose that you may be able to identify them , they will be discussed in much details in other post to help you not only identify them but also work with them.

That’s a wrap! Thank you for reading this post, I hope it was useful. Kindly share your thoughts in the comment section. Click the follow button for useful posts on learning and mastering JavaScript as well as other technologies in web development!.