JavaScript Data Types
JavaScript Data Types
JavaScript provides different data types to hold various kinds of values. There are two main data types in JavaScript.
- Primitive type
- Non-primitive type
Primitive Data Type
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.
Number data type
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
String data type
A string represents textual data. It contains a sequence of characters. For example, "hello", "JavaScript", etc. In JavaScript, strings are surrounded by quotes:
- Single quotes: 'Hello'
- Double quotes: "Hello"
- Backticks: Hello
// 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
Boolean data type
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.
- True: Represents a logical state of being correct or valid.
- False: Represents a logical state of being incorrect or invalid.
let isAvailable = true; if (isAvailable) { console.log("The item is available."); } else { console.log("The item is not available."); } // Output: The item is available.
Undefined data type
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"
Null data type
In JavaScript, null represents no value or nothing. For example,
let text = null; console.log(text); // Output: null
Symbol data type
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)
Non-primitive Data Type
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.
Object data type
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
Array data type
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...
