Types of Data Types in JavaScript

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
Luiz Filipe da Silva's photo

It's very important to understand these concepts. Thanks for your clean explanation with great examples!

Shehu Ibrahim Muhammad's photo

Thanks for your time for reading this and finding it worthy

Royal Lodu's photo

Well, its not that pretty easy as it seems to be but we need to carry out things one more time as expected though and we can also get more knowledge from somewhere like best essay writing service Australia online service to make everything happen on time even if you are in a school and need assignment

Harry Oliver's photo

Thanks for writing a superb Blog. On this website, I always see quality depending articles. I also follow you. I want to be the best blogger like you—every time I like to read your writing stuff because I get very useful content there. You do great work. Cannabis Business Social Network

Eugenia N. Dyar's photo

Wonderful article. It's very useful. As a home tuition service provider, I really appreciate your post. Thanks for sharing this with us. Read More

Jhon Carry's photo

JavaScript is the popular programming language. It is easy to learn. There are eight basic data type in JavaScript. Our technical expertise help to start your startup company within a very short time period Check it how to make a startup company . We have developed own methodology for startup development that help us to complete projects efficiently.

Logan Clark's photo

I think you can be very popular on youtube with this topic. From here https://soclikes.com/ you can get youtube subscribers if you want to try

thewmat's photo

We are always waiting for you at free solitaire with exciting new games that you should experience.

Ryan Porter's photo

I see you know so much about javascript. I think you can show on video how you know it and teach people. You can use tiktok to post your video. If you use professional help to promote your channel you will be very popular. Read about tiktok packages on this site https://blogs.iu.edu/dbauman/2020/05/28/how-to-become-a-successful-tiktoker/

stefan salvatore's photo

First, I have appreciated you for this comment features. Second, I am happy to read your blog and saw all the things. Below I mentioned some counsel about the clean-up software with lots of opportunities in which you could choose the one the best software best software to cleanup your PC

David Mocond's photo

I learned absolutely everything about my question when I read this post, thanks to the author for the detailed description. I wrote my review on the https://essaysservicesreviews.com/domyassignments-com-review/ you can go in and read. Thank you very much for your attention and your time.

altaf hussain's photo

We are the Dubai local-based Samsung washing machine repair Dubai Company that committed to providing their client with helpful information and value-added service at your own budget. We are a reliable company in Dubai especially of washing machine including Samsung washing machine. You are so lucky, we are here to help you with 24/7 hour.

altaf hussain's photo

With so many washing machine repair shops in Dubai international city from which to choose, you can assuredly have a quick, cheap, and hassle free Washing machine repair Dubai International city anywhere in the city, at any time. Simply post a washing machine repair call in international city Dubai, and the supplies will come to you. There are many repair shops in Dubai that service both washing machines and dryers. Whether you want to get your washing machine repaired quickly or you would like it replaced immediately, the technicians at the shop can help you out. They will assess your washing machine repair needs and then provide you with an estimate of the cost. Depending on the make and model of the machine, there might be several spare parts to be bought from them. These technicians have access to a stock of spare parts of all the top brands such as LG, Whirlpool, IBM, etc., so they can put your equipment back in action within no time