Beginner's Guide to Data Types in JavaScript🧑‍🎓

Beginner's Guide to Data Types in JavaScript🧑‍🎓

Understand the Building Blocks of Your Code

JavaScript is a dynamic and versatile programming language that offers a wide range of data types and objects. In this article, we will explore the different types of data types and objects in JavaScript and provide examples of how to use them.

There are two main types of data types in JavaScript: primitive and non-primitive.

1. Primitive data types

Primitive data types are the basic data types that are built into the language. They include:

i. String

A string is a sequence of characters. As an illustration, the code that follows creates the variable "name" and gives it the value "Binod":

let name = "Binod";
console.log(typeof name); // Output: string

ii. Number

A number has a numeric value. For example, the following code creates a variable called "age" and assigns it the value 22:

let age = 22;
console.log(typeof age); // Output: number

iii. Boolean

A boolean value has two possible outcomes: true or false. For example, the following code creates a variable called "isStudent" and assigns it the value true:

let isStudent = true;
console.log(typeof isStudent); // Output: boolean

iv. Symbol

A symbol is a particular kind of unchangeable data. For example, the following code creates a variable called "symbol" and assigns it a new symbol:

let symbol = Symbol();
console.log(typeof symbol); // Output: symbol

v. null

A null value represents an empty value. For example, the following code creates a variable called "empty" and assigns it the value null:

let empty = null;
console.log(typeof empty); // Output: object

vi. Undefined

The undefined value represents a variable that has not been assigned a value. For example, the following code creates a variable called "notDefined" and assigns it the value "undefined":

let notDefined;
console.log(typeof notDefined); // Output: undefined

2. Non-primitive data types

Non-primitive data types, also known as reference types, are more complex data types that are built on top of the primitive data types. They include:

i. Array

An array is a collection of values. For example, the following code creates an array called "numbers" and assigns it the values 1, 2, and 3:

let numbers = [1, 2, 3];
console.log(typeof numbers); // Output: object

ii. Object

A set of attributes and methods make up an object. As an illustration, the code below generates an object named "person" with the values "name" and "age":

let person = {
  name: "Binod";
  age: 22;
};
console.log(typeof person); // Output: object

iii. Function

A chunk of executable code is known as a function. For example, the following code creates a function called "greet" that takes a name as a parameter and returns a greeting:

function greet(name) {
  return "Hello, " + name;
}
console.log(typeof greet); // Output: function

iv. Date

Working with dates and timings requires the usage of a date object. For example, the following code creates a date object and uses the "getFullYear" method to get the current year:

let today = new Date();
console.log(today.getFullYear()); // Output: 2022

In conclusion, JavaScript offers a wide range of data types and objects that can be used to define and manipulate data in a program. Understanding the different data types and objects in JavaScript and how to use them is essential for any JavaScript developer. With the above examples, you can now understand how to create variables with different primitive and non-primitive data types in JavaScript.