Day 4 of JavaScript
Tags: javascript, webdev, beginners, learning
Published: 2024-04-20 15:42:33

Hey there :) in the last post we have understood how comments are used and variables are declared in JavaScript. In this post we are going to see the data types in JavaScript. So let's get started.

JavaScript provides two different types of data types. They are->

1. Primitive Data Types

2. Non Primitive Data Types

Let's see each one of them individually.

> Note -: In JavaScript ,as it is dynamically typed language you don't need to bother about specifying data type of variable ,you just simply have to assign them as `var`, `let` or `const` according to your requirement.

## Primitive Data Types

**Primitive data types are basic data types that are not objects and do not have methods. ** They are immutable and do not have their own methods and properties. When you manipulate a primitive value, you're working directly with its value rather than with a reference to it. Primitive values are stored directly in memory, and when you assign a variable to a primitive value, the variable stores a copy of that value.

There are six primitive data types:

1. **String:** Represents a sequence of characters, such as "hello".

2. **Number:** Represents both integer and floating-point numbers, such as 42 or 3.14.

3. **Boolean:** Represents a logical entity, either true or false.

4. **Null:** Represents null i.e. no value at all.

5. **Undefined:** Represents a variable that has been declared but not assigned a value.

6. **Symbol:** Represents a unique identifier, introduced in ECMAScript 6 (ES6).

## Non Primitive Data Types

**Non-primitive data types in JavaScript are data types that are capable of storing collections of values and have methods and properties.** They are mutable, meaning their values can be changed.
The main non-primitive data types in JavaScript are:

1. **Object:** Objects are complex data types that can hold key-value pairs. They are collections of properties where each property has a name and a value. Objects can represent various entities and can contain functions, arrays, and other objects as values.
`const person = {
    name: "John",
    age: 30,
    hobbies: ["reading", "coding"],
    address: {
        city: "New York",
        country: "USA"
    },
    sayHello: function() {
        console.log("Hello!");
    }
};`
Here person is object having name,age,hobbies and address as properties and `sayHello` as method.

2. **Array:** Arrays are ordered collections of values. They can store multiple values of any type, including other arrays and objects. Arrays are indexed, meaning each value is associated with a numeric index starting from 0.
`const fruits = ["apple", "banana", "orange"];`

3. **Function:** Functions are a special type of object that can be invoked to perform a task or calculate a value. Functions can be assigned to variables, stored in objects or arrays, and passed as arguments to other functions.
`function add(a, b) {
    return a + b;
}`

4. **Date:** The `Date` object represents a specific date and time in JavaScript. It provides methods for working with dates and times, such as getting the current date, formatting dates, and performing arithmetic operations on dates.
`const currentDate = new Date();`

So this was it for this post in the next post we will be reading about operators and conditional statements. Till then stay connected and don't forget to follow me :)