Home >Web Front-end >Front-end Q&A >What data type does javascript have?

What data type does javascript have?

PHPz
PHPzOriginal
2023-05-20 21:53:37602browse

Javascript is a dynamically typed language, so its data types are very flexible. In Javascript, data types can be basic types or object types. This article will introduce various data types in Javascript.

1. Basic data types

  1. String

String is one of the most common types in Javascript. It represents a text value, enclosed in single or double quotes. Strings can contain any characters, including spaces and special characters. For example:

var str1 = 'Hello world!';
var str2 = "My name is Jack.";
  1. Number

The number type represents a numeric value. It includes integers, floating point numbers and NaN. For example:

var num1 = 10;
var num2 = 3.14;
var num3 = NaN;
  1. Boolean value (Boolean)

The Boolean type has only two values, true and false, representing true and false. For example:

var bool1 = true;
var bool2 = false;
  1. null

null represents a null value or a non-existent object. If the value of a variable is null, it means that the variable has no value. For example:

var x = null;
  1. undefined

undefined represents an undefined value. If a variable has no value assigned, its value is undefined. For example:

var y;
console.log(y); // undefined

2. Object types

In addition to basic types, Javascript also has some complex types, called object types. An object is a complex data structure that can contain multiple properties and methods.

  1. Object (Object)

Object is one of the most important data types in Javascript. It is a complex structure that can contain multiple properties and methods. Objects are represented by {} and consist of comma-separated key-value pairs. Keys are represented by strings and values ​​can be any type of data. For example:

var person = {
    name: 'Jack',
    age: 30,
    gender: 'male'
};
  1. Array

Array is an object type used to store an ordered set of data. Represented by [] and consisting of comma-separated values. The values ​​in the array can be any type of data. For example:

var fruits = ['apple', 'banana', 'orange'];
  1. Function

A function is a special object used to execute blocks of code. It can receive parameters and can return values. Functions can be defined as variables or properties, or via the function keyword. For example:

function add(a, b) {
    return a + b;
}
  1. Date (Date)

JavaScript’s Date object is used to process dates and times. It provides a set of methods to get and set the date and time. For example:

var today = new Date();
console.log(today.toDateString()); // 输出:Mon Aug 16 2021
  1. Regular Expression (RegExp)

Regular expression is an object type used to match string patterns. It consists of a pattern string and some flags used to specify matching rules. For example:

var pattern = /Hello/;
var str = 'Hello world';
console.log(pattern.test(str)); // 输出:true

To sum up, Javascript’s data types are very flexible, including basic data types and object types. Understanding the characteristics and usage of each data type can help us write better Javascript code.

The above is the detailed content of What data type does javascript have?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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