Home Web Front-end JS Tutorial JavaScript Data Types

JavaScript Data Types

Sep 29, 2024 pm 10:26 PM

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.

  1. Primitive type
  2. 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
Copy after login

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
Copy after login

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.
Copy after login

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"
Copy after login

Null data type

In JavaScript, null represents no value or nothing. For example,

let text = null;
console.log(text);  // Output: null
Copy after login

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)
Copy after login

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
Copy after login

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' ]
Copy after login

The above is the detailed content of JavaScript Data Types. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

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...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

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.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

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 using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

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...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

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.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

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 Evolution of JavaScript: Current Trends and Future Prospects The Evolution of JavaScript: Current Trends and Future Prospects Apr 10, 2025 am 09:33 AM

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.

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

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. �...

See all articles