JavaScript is a high-level, interpreted programming language primarily used for creating interactive and dynamic content on webpages. It was initially developed by Netscape Communications Corporation in collaboration with Sun Microsystems in 1995. JavaScript is now one of the core technologies of web development along with HTML and CSS.
In JavaScript, there are several data types that can be used to represent different kinds of values. Here's an overview of the basic data types in JavaScript:
1. Primitive Data Types:
- Number: Represents numeric values, including integers and floating-point numbers.
- String: Represents sequences of characters enclosed in single quotes ('') or double quotes ("").
- Boolean: Represents true or false values.
- Undefined: Represents a variable that has been declared but not initialized.
- Null: Represents the intentional absence of any value.
- Symbol: Represents unique identifiers that are used as property keys in objects.
2. Non-primitive Data Types:
- Object: Represents a collection of key-value pairs where keys are strings (or symbols) and values can be any data type, including other objects.
- Array: Represents an ordered collection of values, typically indexed by integers starting from 0.
- Function: Represents executable code that can be invoked with zero or more arguments.
3. Special Data Types:
- NaN (Not-a-Number): Represents a value that is not a valid number.
- Infinity and -Infinity: Represents positive and negative infinity, respectively.
- BigInt: Represents integers with arbitrary precision, introduced in ES2020.
Examples:
// Number
let num = 10;
// String
let str = "Hello, World!";
// Boolean
let isTrue = true;
// Undefined
let undef;
// Null
let nul = null;
// Symbol
let sym = Symbol("unique");
// Object
let obj = { key: "value" };
// Array
let arr = [1, 2, 3];
// Function
function greet(name) {
console.log("Hello, " + name + "!");
}
//NaN
let result = "hello" / 2;
console.log(result); // Output: NaN
//Infinity and -Infinity
console.log(1 / 0); // Output: Infinity
console.log(-1 / 0); // Output: -Infinity
//BigInt
let bigNum = 1234567890123456789012345678901234567890n;
console.log(bigNum); // Output: 1234567890123456789012345678901234567890n