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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
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.

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.

JavaScript Engines: Comparing Implementations JavaScript Engines: Comparing Implementations Apr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

JavaScript: Exploring the Versatility of a Web Language JavaScript: Exploring the Versatility of a Web Language Apr 11, 2025 am 12:01 AM

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration) How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration) Apr 11, 2025 am 08:22 AM

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

From C/C   to JavaScript: How It All Works From C/C to JavaScript: How It All Works Apr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Building a Multi-Tenant SaaS Application with Next.js (Backend Integration) Building a Multi-Tenant SaaS Application with Next.js (Backend Integration) Apr 11, 2025 am 08:23 AM

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

See all articles