Home >Web Front-end >JS Tutorial >What are the basic data types in js

What are the basic data types in js

王林
王林Original
2024-02-19 20:22:231255browse

What are the basic data types in js

What are the basic data types in js, specific code examples are required

JavaScript is a programming language widely used in web development. In JavaScript, there are several basic data types, including Number, String, Boolean, Undefined, Null and Symbol. This article walks through each of these data types and provides specific code examples.

  1. Number (Number)
    Number is the data type representing numbers in JavaScript. It can represent integers or floating point numbers. Here is a sample code:
var num1 = 10; // 整数
var num2 = 3.14; // 浮点数

console.log(num1); // 输出:10
console.log(num2); // 输出:3.14
  1. String (String)
    String is a data type that represents text in JavaScript. Strings can be declared using single or double quotes. Here is a sample code:
var str1 = 'Hello'; // 使用单引号声明字符串
var str2 = "World"; // 使用双引号声明字符串

console.log(str1 + ' ' + str2); // 输出:Hello World
  1. Boolean (Boolean value)
    Boolean is a data type in JavaScript that represents true or false. It has only two values: true and false. The following is a sample code:
var bool1 = true; // 真
var bool2 = false; // 假

console.log(bool1); // 输出:true
console.log(bool2); // 输出:false
  1. Undefined (undefined)
    Undefined is a special value in JavaScript, indicating that the variable has not been assigned a value. The following is a sample code:
var x; // 未定义变量x

console.log(x); // 输出:undefined
  1. Null (null value)
    Null is a special value in JavaScript, indicating that the value of the variable is empty. The following is a sample code:
var y = null; // 变量y的值为空

console.log(y); // 输出:null
  1. Symbol (symbol)
    Symbol is a new data type in JavaScript, representing a unique identifier. The following is a sample code:
var sym = Symbol('foo'); // 创建一个Symbol类型的值

console.log(sym); // 输出:Symbol(foo)

The above are several basic data types in JavaScript and corresponding code examples. Mastering these data types is very important for writing JavaScript programs. I hope this article can be helpful to readers.

The above is the detailed content of What are the basic data types in js. 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