Javascript - Data Types

Javascript - Data Types

JavaScript :- The Heart of Web Development

Javascript is a programming language that powers the web. From dynamic user interfaces to making API requests, Javascript plays a vital role in web development. In this blog, we will explore the different data types in Javascript, what they are, and how to use them. We'll also try to make it as engaging and interesting as possible, with code snippets to help illustrate the concepts.

1 - Number

The number data type in Javascript can represent both integer and floating-point numbers. For example, 42, 3.14, and -15 are all numbers in Javascript.

let num1 = 42;
let num2 = 3.14; 
let num3 = -15;

console.log(typeof num1); // "number" 
console.log(typeof num2); // "number"
console.log(typeof num3); // "number"

2 - String

A string is a sequence of characters, represented in quotes (either single or double). Strings are often used to represent text, such as a person's name or a product's description.

let name = "John Doe";
let product = "Apple iPhone";

console.log(typeof name); // "string"
console.log(typeof product); // "string"

3 - Boolean

A boolean data type can have only two values: true or false. This data type is often used in conditional statements to control the flow of a program.

let isTrue = true;
let isFalse = false;

console.log(typeof isTrue); // "boolean"
console.log(typeof isFalse); // "boolean"

4 - Undefined

A variable with the value undefined means that the variable has been declared, but has not been assigned a value yet.

let animal;

console.log(typeof animal); // "undefined"

5 - Null

The value null represents an intentional non-value. It can be used to explicitly set a variable to an "empty" value.

let nothing = null;

console.log(typeof nothing); // "object"

6 - Object

An object in Javascript is a collection of key-value pairs. The keys are used to identify values, which can be of any data type, including numbers, strings, booleans, and even other objects.

let person = {
  firstName: "John",
  lastName: "Doe",
  age: 42,
  isMarried: true
};

console.log(typeof person); // "object"

7 - Symbol

A symbol is a new data type introduced in ECMAScript 6 (the latest version of Javascript). It is a unique and immutable data type, which means that it cannot be changed once created. Symbols are often used as property keys in objects to avoid naming collisions with other keys.

let symbol1 = Symbol("Hello");
let symbol2 = Symbol("Hello");

console.log(typeof symbol1); // "symbol"
console.log(symbol1 === symbol2); // false

8 - BigInt

All JavaScript numbers are stored in a 64-bit floating-point format.

JavaScript BigInt is a new datatype (2020) that can be used to store integer values that are too big to be represented by a normal JavaScript Number.

let x = BigInt("123456789012345678901234567890");

console.log(x) // 123456789012345678901234567890n

That's it! These are the 8 data types in Javascript. Understanding these data types is crucial for writing efficient and effective code. Now that you have a good understanding of data types, try to use them in your code.

Happy Coding!