Understanding Data Types in JavaScript

Understanding Data Types in JavaScript

In JavaScript, every value has a data type that determines its nature and behavior when manipulated. These data types can be broadly categorized into two main groups: Primitive data types and Non Primitive data types.

Primitive Data Types:

Primitive data types are the building blocks of JavaScript. They cannot be changed and directly hold a value. There are six primitive data types in JavaScript:

1. Number:-

Represents numeric data, including integers and floating-point numbers.

const myNum = 12345;

console.log("the number is",myNum);

console.log(typeof myNum);

2. String:-

Represents textual data, enclosed within single (' ') or double (" ") quotes.

const myName = "MAHI";

console.log("Name",myName);

console.log(typeof myName);

3. Boolean:-

Represents a logical value, either true or false.

const loggedIn = false;

console.log("the value is",loggedIn);

console.log(typeof loggedIn);

4. Null:-

Represents an intentional absence of any object value.

const emptyValue = null;

console.log("the value ",emptyValue);

console.log(typeof emptyValue);

5. Undefined:-

Represents a variable that has been declared but not assigned a value.

let ourName;

console.log(ourName);

console.log(typeof ourName);

Difference between null and undefined:-

Null:- It is a representation of empty value.

Undefined:- Undefined on the other hand means the value is not defined yet.

6. Symbol:-

Symbol is a built-in object whose constructor returns a symbol primitive — also called a Symbol value or just a Symbol — that's guaranteed to be unique.

const sym1 = Symbol("hey");

console.log(sym1);

console.log(typeof sym1);

In conclusion, understanding data types, is fundamental for writing robust JavaScript code. It not only helps in better memory management but also ensures that your code behaves predictably and is easier to debug.