Home > Article > Web Front-end > JavaScript Data Types
JavaScript provides different data types to hold various kinds of values. There are two main data types in JavaScript.
The predefined data types provided by JavaScript, are known as primitive data types. Primitive data types are also known as in-built data types. They can hold a single simple value.
String, Number, BigInt, Boolean, undefined, null, and Symbol are primitive data types in JavaScript.
The number type in JavaScript contains both integer and floating-point numbers. Besides these numbers, we also have some special numbers in JavaScript such as Infinity, -Infinity, and NaN (Not-a-Number).
let x = 20; let y= 15; console.log(x + y); // Output: 35 console.log(typeof (x + y)); // Output: number
A string represents textual data. It contains a sequence of characters. For example, "hello", "JavaScript", etc. In JavaScript, strings are surrounded by quotes:
// string enclosed within single quotes let language = 'JavaScript'; console.log(language) // Output: JavaScript // string enclosed within double quotes let frameWork = "React"; console.log(frameWork); // Output: React // string enclosed within backticks let message = `${frameWork} is a ${language} framework`; console.log(message); // Output: React is a JavaScript framework
In JavaScript, the Boolean data type represents a logical entity. That has only two values: true or false. Boolean values are usually used in conditional statements like if, else, while, and ternary operators to control the flow of executions based on certain conditions.
let isAvailable = true; if (isAvailable) { console.log("The item is available."); } else { console.log("The item is not available."); } // Output: The item is available.
In JavaScript, undefined is a special data type and value that indicates a variable has been declared but has not yet been assigned a value. It represents an "uninitialized" or "unknown" state. The type of undefined is undefined.
let x; console.log(x); // Output: undefined console.log(typeof x); // Output: "undefined"
In JavaScript, null represents no value or nothing. For example,
let text = null; console.log(text); // Output: null
The Symbol data type is a unique and immutable primitive value, introduced in ES6 (ECMAScript 2015). Symbols are primarily used as unique identifiers for object properties, ensuring that no property keys conflict, even if they have the same name.
let symbol1 = Symbol(); let symbol2 = Symbol("description"); let symbol3 = Symbol("description"); console.log(symbol1); // Output: Symbol() console.log(symbol2); // Output: Symbol(description) console.log(symbol2 === symbol3); // Output: false (Each symbol is unique)
The data types derived from primitive data types of the JavaScript language are known as non-primitive data types. It is also known as derived data types or reference data types. They can hold multiple values. Non-primitive types include Object, Array, and RegExp.
In JavaScript, an object is a collection of related data and functions, known as properties and methods. Properties are “key: value” pairs that store data, while methods are functions associated with the object that can manipulate its properties.
let person = { name: "John Doe", age: 30, isEmployed: true, greet: function() { console.log("Hello, my name is " + this.name); } }; console.log(person.name); // Output: John Doe person.greet(); // Output: Hello, my name is John Doe
In JavaScript, an Array is a special-form object used to store multiple values in a single variable. It can hold various data types and allows for dynamic resizing. Elements are accessed by their index, starting from 0.
// Creating an Array and Initializing with Values let courses = ['HTML', 'CSS', 'JavaScript', 'React']; console.log(courses); // [ 'HTML', 'CSS', 'JavaScript', 'React' ]
The above is the detailed content of JavaScript Data Types. For more information, please follow other related articles on the PHP Chinese website!