DEV Community

G Gokul
G Gokul

Posted on

DATA TYPES IN JAVASCRIPT

Data in js:

  • In JavaScript, data refers to the values that variables can store and manipulate in a program.
  • These values have data types, which define how the data is stored in memory and how it behaves during operations.
  • JavaScript data types define what kind of values a variable can hold and how those values behave in a program.
  • They determine how data is stored in memory and how operations like comparison, calculation, and conversion work.
  • Understanding data types helps prevent errors and makes code more efficient and reliable.

Types of Data in JavaScript:

  • JavaScript data types are categorized into Primitive and Non-Primitive types

data types

Primitive Data Type:

  • Primitive data types in JavaScript represent simple, immutable values stored directly in memory, ensuring efficiency in both memory usage and performance.
  • These store single values and are immutable (cannot be changed directly).
  • String, Number, BigInt, Boolean, undefined, null, and Symbol are primitive data types.

1.String:

  • It represents textual data enclosed in single, double, or backticks.
  • It contains a sequence of characters.

example:
let name = "John";
console.log(name, typeof name);

output:
John string

2.Number:

  • The Number data type in JavaScript includes both integers and floating-point numbers.
  • Special values like Infinity, -Infinity, and NaN represent infinite values and computational errors, respectively.

example:(TBD)
let n1 = 2;
console.log(n1)
let n2 = 1.3;
console.log(n2)
let n3 = Infinity;
console.log(n3)
let n4 = 'something here too' / 2;
console.log(n4)

output:
2
1.3
Infinity
NaN

3.Boolean:

  • The boolean type has only two values i.e. true and false.

example:
let b1 = true;
console.log(b1);
let b2 = false;
console.log(b2);

output:
true
false

4.Undefined:

  • A variable that has been declared but not initialized with a value is automatically assigned the undefined value.
  • It means the variable exists, but it has no value assigned to it.

example:
let name;
console.log(name);

output:
undefined

5.Null:

  • In JavaScript, null represents "no value" or "nothing."

example:
let number = null;
console.log(number);

output:
null

6.Symbol:

  • Symbols, introduced in ES6, are unique and immutable primitive values used as identifiers for object properties.
  • They help create unique keys in objects, preventing conflicts with other properties.

example:
// two symbols with the same description
let value1 = Symbol("programiz");
let value2 = Symbol("programiz");
console.log(value1 === value2);

output:
false

7.BigInt:

  • BigInt is a built-in object that provides a way to represent whole numbers greater than 253.
  • The largest number that JavaScript can reliably represent with the Number primitive is 253, which is represented by the MAX_SAFE_INTEGER constant.

example:
let value1 = 900719925124740998n;
let result1 = value1 + 1n;
console.log(result1);

output:
900719925124740999n

Non-Primitive Data Types:(to be discussed)

  • The data types that are derived from primitive data types are known as non-primitive data types.
  • It is also known as derived data types or reference data types.

1.Object
2.Array
3.Function
4.Data Object
5.Regular Expression

References:


JavaScript Data Types - GeeksforGeeks

Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more.

favicon geeksforgeeks.org

Top comments (0)