Understanding Data Types in Javascript

Understanding Data Types in Javascript

Be brilliant at the basics is a popular saying any successful person/programmer should live by because understanding the basics is a great foundation to building great projects.

Computer programs works basically by manipulating different kinds of values. Irrespective of the programming language, these values are fundamental characteristics of the language and are known as Types.

This article attempts to list the built-in data structures available in JavaScript and what properties they have.

We will go over how data types work in JavaScript as well as the important data types native to the language. This is not an exhaustive investigation of data types, but will help you become familiar with the options available to you in JavaScript.

Strings

A string is a sequence of one or more characters (letters, numbers, symbols). Strings are useful in that they represent textual data. In JavaScript, strings exist within either single quotes ' or double quotes ", so to create a string, enclose a sequence of characters in quotes:

const singleQuotes = 'Hello there.'; //using single quotes
const doubleQuotes = "Hello there."; //using double quotes

You can include quotes inside the string as long as they don't match the enclosing quotes.

const a = 'We\'ll never give up.'; // escaping single quote with backslash

There are many operations that we can perform on strings within our programs in order to manipulate them to achieve the results we are seeking.

Numbers

The number data type can be written in JavaScript with or without decimals and also written using exponential notation e.g. 3.5e-4 (equivalent to 3.5x10-4).

const a = 55;         // integer
const b = 81.5;       // floating-point number
const c = 2.25e-4;    // exponential notation, same as 0.000225

In addition to representing numbers, the JavaScript number type also has three symbolic values available:

Infinity — a numeric value that represents a positive number that approaches infinity.

const num = 5 / 0;   // will return Infinity

-Infinity— a numeric value that represents a negative number that approaches infinity.

const num = -5 / 0;  // will return -Infinity

NaN — a numeric value that represents a non-number, standing for not a number.

const a = 10 / "Shark";   // a will be NaN

Boolean

Boolean represents a logical entity and can have two values: true and false.

const isReading = true;   //yes, I'm reading
const isFalse = 5 > 8; //false

Undefined

A variable that has not been assigned a value has the value undefined.

let a;
let b = "Hello World!"

console.log(a) // Output: undefined
console.log(b) // Output: Hello World!

Null

The Null data type can have only one value-the null value. A null value means that there is no value. It is not equivalent to an empty string ("") or 0, it is simply nothing.

let a = null;
console.log(a); // Output: null

Object

The object is a complex data type that allows you to store collections of data.The JavaScript object data type can contain many values as name:value pairs. The object literal syntax is made up of name:value pairs separated by colons with curly braces on either side { }.

let car = {
    modal: "BMW X3",
    color: "white",
    doors: 5
}

Array

Arrays are regular objects for which there is a particular relationship between integer-key-ed properties and the length property. Just as strings are defined as characters between quotes, arrays are defined by having values between square brackets [ ]. The array index starts from 0, so that the first array element is arr[0] not arr[1].

let cities = ["London", "Abuja", "New York"];
console.log(cities[2]); // Output: New York

Functions

Functions are regular objects with the additional capability of being callable. Since functions are objects, so it is possible to assign them to variables.

let greeting = function(){ 
    return "Hi there!"; 
}

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

The typeof Operator

The typeof operator can be used to find out what type of data a variable or operand contains.

typeof 15;  // Returns: "number"
typeof Infinity;  // Returns: "number"
typeof '12';  // Returns: "string". Number within quotes is typeof string
typeof Null;  // Returns: "object"
typeof [1, 2, 4];  // Returns: "object"
typeof function myFunc(){}   // Returns "function"

For more learning on data types in Javascript, visit

Conclusion

This should give a better understanding of some of the major data types that are available for you to use in JavaScript. Each of these data types will be essential as you develop programming projects in the JavaScript language. Feel free to like, share and comment :) .