Javascript Data types

Javascript Data types

A beginner's guide through Javascript basics

Introduction

JavaScript is a loosely typed and dynamic language.

WTH does that mean, nerd? 🥴 Well, consider this example:

let age = 15;      // age is of type number
age = "Bello ololade" ;    // age is of type string

Now that's sloppy right? Meaning the variable "age" can accept any type of data, be it "number", "string" even Boolean. It's simply not associated with a specific data type!

Data types

Now that brings us to data types. This is an important concept in every programming language. Working on variables will be difficult for the computer, if the data type is unknown. Example:

let user = 17 + "Ololade"  //Expected output: 17Ololade

Yeah, it dosen't make sense to me too🙄

In Javascript there are 8 data types, they are:

  1. Strings
  2. Numbers
  3. Boolean
  4. Null
  5. Undefined
  6. Bigint
  7. Symbol
  8. All the above types are collectively termed as primitive data types.

    Relax, we'll that in a sec😉

  9. Objects

Primitive Data types? 🤔

All types except objects define immutable values (that is, values which can't be changed). Example:

let fullName = "Bello Ololade";
console.log(fullName);    // Bello Ololade

fullName.toUpperCase();
console.log(fullName);   // Bello Ololade

A primitive can be replaced, but it can't be directly altered.

Strings

A string is a series of characters E.g "Bello Ololade".

Strings are written with quotes. Both single or double quotes are allowed. Also backticks (` `) can be used. Example:

//string examples
const firstName = 'Ayo';

const lastName = "Olo";

const fullName = `The names are ${name} and ${name1}`;
console.log(fullName)  //The names are Ayo and Olo

Single quotes and double quotes are almost the same and you can use either of them.

Backticks are generally used when you need to include variables or expressions into a string. This is done by wrapping variables or expressions with ${variable or expression} as shown above.

Numbers

Yeah, you guessed it! Its thesame as your boring old numbers🥴

let age = 55;    // without decimals
let size = 12.5;  // with decimals

Notice the absence of quotes (" ")? Yeah that wasn't a mistake! Numbers don't need quotes.

Boolean

Booleans can only have two values: true or false.

Example:

var isOnline = false;

var isOld = true;

Null

Null is a special value that represents empty or unknown value.

For example:

 let age = null;

This means the age variable is empty or null.

Undefined

Variables that has no value is called undefined.

Example:

let name;       // "value and type is not defined"
console.log(name);  // Expected output: undefined

You can also empty a value by equating it to undefined!

let age = undefined;
console.log(age);  //Expected output: undefined

Bigint

Bigint are used for larger numbers, larger than (2⅝³-1).

A BigInt number is created by appending n to the end of an integer. For example,

// BigInt value
const value1 = 400719925124740998n;

// Adding two big integers
const result1 = value1 + 1n;
console.log(result1); // "400719925124740999n"

const value2 = 900719925124740998n;

// Error! BitInt and number cannot be added
const result2 = value2 + 1;
console.log(result2);

Symbol

Symbol data type was introduced in a newer version of JavaScript (from ES2015).

A value having the data type Symbol can be referred to as a symbol value. Symbol is an immutable primitive value that is unique. For example,

// two symbols with the same description

const greeting1 = Symbol('hi');
const greeting2 = Symbol('hi');

Though greeting1 and greeting2 both contain 'hello', they are different as they are of the Symbol type.

Objects

An object is a complex data type that allows us to store collections of data. JavaScript objects are written with curly braces {}. Object properties are written as name:value pairs, separated by commas. Example:

const user = {
    firstName: 'Ololade',
    lastName: null,
    age: 10,
};

The typeof operator

To find the type of a variable, you can use the typeof operator.

The typeof operator returns the type of a variable or an expression. For example:

const name = 'Ayo';
typeof(name); // returns "string"

const age = 4;
typeof(age); //returns "number"

const isOnline = true;
typeof(isOnline); //returns "boolean"

Summary

There's no summary, you impatient nerd! 🥴 Now go back and finish it! Thank you for reading