javascript

Data Types


 

In JavaScript, data types categorize the various kinds of values that a program can manipulate. The language distinguishes between primitive data types, including numbers, strings, booleans, null, undefined, and symbols, each representing simple values, and reference data types such as objects, arrays, functions, dates, and regular expressions that represent more complex and structured data.

  • Primitive Data Types:
  • Non-Primitive Data Types:

 

 ❑ Primitive Data Types:

Primitive data types are also known as in-build data types which are immutable and  passed by value.

 

There are six primitive data types as follows:

 

1. Numbers

It represents numeric values, such as integers or floating-point numbers.

let age = 23;

 

2. Strings

It represents sequences of characters.

let str = "hello Coders";

 

3. Booleans

It represents either true or false.

let boolValue =false;

 

4. Null

It represents absence of value.

let nullValue = null;

 

5. Undefined

It represents an uninitialized variable or absence of value.

let undefinedValue = undefined;

 

6. Symbols

It repensents a unique identifier.

let syb = Symbol("unique");

 

 

 ❑ Non-Primitive Data Types:


Non-primitive data types are referred to complex structures that can store and organize multiple values.
 

1. Object:

An object is a collection of key-value pairs, allowing you to create complex data structures and models. Keys (properties) can be strings or symbols, and values can be any data type.

 

Syntax:

let object_variable = {
        key1: value1,
        key2: value2,
        // Additional key-value pairs
	};

 

Example:

let myobject = {
        userName: "Wasif",
        age: 21,
    }

 

2. Array:

An array is an ordered list that can hold multiple values. Each value in the
array element is identified by an index, starting from 0.
 

Syntax:

let array_variable = [value1, value2, value3];

 

Example:

let array_name = [value1, value2, value3];

 

3. Function

A function is a block of reusable code that can be invoked with different arguments. Functions can return values and are a fundamental building block in JavaScript.

 

Syntax:

function function_name(paraMeter){
	// code
}

 

Example:

function product(){
		let a = 3;
		let b = 4;
		let product = a * b;
	}