Shehu Ibrahim Muhammad
Shehu-Hikmah

Shehu-Hikmah

Types of Data Types in JavaScript

Types of Data Types in JavaScript

Shehu Ibrahim Muhammad's photo
Shehu Ibrahim Muhammad

Published on Aug 2, 2020

6 min read

Subscribe to my newsletter and never miss my upcoming articles

JavaScript is one of topmost programming language used both within and outside the browser. It is mostly used for web development, as a scripting language just like others it has it own syntax rules and other attributes, in this article we will be looking at the various data types available in JavaScript which allows storage and manipulation of data.

JavaScript data types stipulate what kind of data can be stored and manipulated within its context.

JavaScript is a dynamic type language, which means you can declare and assign the multiple values to a variable; the values assigned are dynamically passed using the various data types available in JavaScript. These values could be strings or numbers etc. as demonstrated below

var n;           // the variable n is undefined
n = 10;           // the variable n is a Number
n  = "Office";      // the variable n is a String

Any programming language you will be working it, it is important to know its data types, in other to be able to solve complex problems properly.

Furthermore, JavaScript needs to have each variable assigned data a type. This type stipulates what can or cannot be done with the data.

Types of JavaScript Data Types

js1.png

We have about eight (8) data types in JavaScript, which is divided into three types

PRIMITIVE DATA TYPE

A primitive data value is a single simple data value with no additional properties and methods. They only accept or hold one data at a time.

  1. STRING- represents sequence of characters e.g. "hello".

  2. NUMBER- represents numeric values e.g. 100.

  3. BOOLEAN- accept only two value either false or true.

THE COMPOSITE DATA TYPES

composite data types are also refer to as reference or non-primitive data types, it can hold collections of values and more complex entities.

  1. ARRAY- represents group of similar values.

  2. OBJECT- represents instance through which we can access members.

  3. FUNCTION- represent any Block of codes callable.

SPECIAL DATA TYPES

These are data types that hold empty values

  1. NULL- represents null i.e. no value at all.

  2. UNDEFINED- represents undefined value.

Let’s discuss each with relevant examples

1. THE STRING DATA TYPE

JavaScript strings are use to represent alphabetical data (i.e. sequences of characters). Strings are created with the characters like alphabets [a-z], other special characters and numerals written between double quotes [ 0-9] when enclosed between a single ('') or double ("") quotes becomes string, as shown below:

var countryName = "Nigeria";   // Using double quotes
var countryName = 'Nigeria';   // Using single quotes

2. THE NUMBER DATA TYPE

The number data type in JavaScript is used to represent numerals both positive and negative numbers, integers or decimals, or large numbers written using exponential notation.

var w = 34.5;     // Written with decimals (float)
var x = 34;        // Written without decimals
var y = 23e5;      // 2300000
var z = 23e-5;     // 0.00023

3. THE BOOLEAN DATA TYPE

The JavaScript Boolean is a logical data type use to hold only two values: true or false. It is mainly used to store values like yes (true) or no (false), on (true) or off (false), etc. as demonstrated below:

var isLearning = true;   // yes, I'm learning
var isWriting = false; // no, I'm not writing

4. THE ARRAY DATA TYPE

An array is a type of JavaScript object used to store multiple values in single variable. Each individual value (also called an element) in an array has a numeric position, known as its index staring from 0 to the end of the array, and JavaScript Arrays Accept any data type, numbers, strings, booleans, functions, objects, and even other arrays. JavaScript Arrays is created and enclosed between two square brackets [....]. As shown in the example below:

var fruits = ["Apple", "Mango", "Lemon"];
// You can also create an array using the new Array() method.

5. THE OBJECT DATA TYPE

JavaScript object is a complex data type with properties, and characteristics just like normal object like car, chair etc. that allows you to store different collections of data. Passed between two curly brackets {....} with property key (name) and value. The property key and value is separated by semicolon : having Property key on left hand side and value on the right hand side. Below is an example:

var person = {
firstName:"James", 
middleName:"Johnson",
lastName:"Abraham", 
age:50, 
nationality:"Nigeria",
eyeColor:"blue"
};
//You can also create an array using the new Object() method.

6. THE FUNCTION DATA TYPE

JavaScript function is callable object that executes a block of code enclosed between curly brackets {.....}. functions is also objects, this make it possible to assign them to variables objects, and arrays, as shown in the example below:

var greeting = myFunc(){ 
    return "Hello Good!"; 
}

7. THE NULL DATA TYPE

The JavaScript Null have only one value. A variable with null value means that there is no value assigned to variable or the string is empty ("") or 0.

var person = null;           // Value is null, but type is still an object
var x = "Hello World";           // the output will be hello world
var x = null;           // Value is now null, but type is still an object

8. THE UNDEFINED DATA TYPE

The undefined data type only have one value, the special value undefined. If a variable has been declared, without assigned value, JavaScript passed the undefined value.

var person;                  // Value is undefined, type is undefined

Both undefined and null are equal in value but different in type:

THE TYPEOF

You can use the JavaScript typeof operator to find the type of a JavaScript variable. The typeof operator returns the type of a variable or an expression:

typeof "John"              // Returns "string"
typeof 3.81                // Returns "number"
typeof true                // Returns "boolean"
typeof false               // Returns "boolean"
typeof x                   // Returns "undefined" (if x has no value)
typeof {name:'John', age:54} // Returns "object"
typeof [1,2,3,4]             // Returns "object" (not "array", because array is also an object)
typeof null                  // Returns "object"
typeof function myFunc(){}   // Returns "function"

You can read from from this articles by MDN

  1. JavaScript data types and data structures
  2. Grammar and types
 
Share this