JavaScript Data Types

Muhammad Hassan
3 min readApr 4, 2021

JavaScript is both dynamically typed and weakly typed language.

Statically Typed

JavaScript is not statically typed unless you’re using a language, tool such as Typescript or Flow that compiles to JavaScript code. Statically typed means the type is enforced and won’t change so easily. All variables must be declared with a type.

int a = 1
string b = 'abc'

Dynamically Typed

Dynamically typed languages infer variable types at runtime. This means once your code is run the compiler will see your variable and its value then decide what type it is.

var a = 1 // int
b = 'abc' // string

Weakly Typed

Weakly typed languages allow types to be inferred as another type. For example, 1 + '2' // '12' In JS it sees you’re trying to add a number with a string. so it changes your number into a string and the result is ‘12’.

JavaScript provides different data types to hold different types of values. There are six basic data types in JavaScript which can be divided into three main categories:

primitive (or primary)

  1. String
  2. Number
  3. Boolean

special data types.

  1. Undefined
  2. Null

composite (or reference)

  1. Object
  2. Array
  3. Function

Objects such as functions and arrays are referred to as non-primitive values. The difference between primitives and non-primitive is that primitives are immutable and non-primitive are mutable. Primitive data types can hold only one value at a time, whereas composite data types can hold collections of values.

String

A string in JavaScript is surrounded by quotes.

In JavaScript, there are 3 types of quotes.

  1. Double quotes: "Hello".
  2. Single quotes: 'Hello'.
  3. Backticks: `Hello`.

Double and single quotes are “simple” quotes. There’s practically no difference between them in JavaScript.

Backticks are “extended functionality” quotes. They allow us to embed variables and expressions into a string by wrapping them in ${…}, for example:

let name = "John";// embed a variable
alert( `Hello, ${name}!` ); // Hello, John!
// embed an expression
alert( `the result is ${1 + 2}` ); // the result is 3

Number

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

let n = 123;
n = 12.345;

Boolean

The boolean type has only two values: true and false.

The “null” value

It’s just a special value that represents nothing, empty or value unknown.

The “undefined” value

The meaning of undefined is “value is not assigned”.

Object

The object is a complex data type that allows you to store collections of data. An object contains properties, defined as a key-value pair. A property key (name) is always a string, but the value can be any data type, like strings, numbers, booleans, or complex data types like arrays, function, and other objects.

var emptyObject = {};
var person = {"name": "Clark", "surname": "Kent", "age": "36"};

// For better reading
var car = {
"modal": "BMW X3",
"color": "white",
"doors": 5
}

Array

An array is a type of object used for storing multiple values in a single variable. Each value in an array has a numeric position, known as its index, and it may contain data of any data type-numbers, strings, booleans, functions, objects, and even other arrays. The array index starts from 0 so that the first array element is arr[0] not arr[1].

var colors = ["Red", "Yellow", "Green", "Orange"];
var cities = ["London", "Paris", "New York"];

alert(colors[0]); // Output: Red
alert(cities[2]); // Output: New York

Function

A function is a callable object that executes a block of code. Since functions are objects, so it is possible to assign them to variables.

var greeting = function(){ 
return "Hello World!";
}

// Check the type of greeting variable
alert(typeof greeting) // Output: function
alert(greeting()); // Output: Hello World!

Functions can be used at any place any other value can be used. Functions can be stored in variables, objects, and arrays. Functions can be passed as arguments to other functions, and functions can be returned from functions.

Resources:

--

--