// JavaScript 数据类型示例
// 1. 基本数据类型 (Primitive Types)
// 1.1 数值型 (Number)
let age = 25;
console.log(typeof age); // "number"
// 1.2 字符串型 (String)
let name = "Alice";
console.log(typeof name); // "string"
// 1.3 布尔型 (Boolean)
let isStudent = true;
console.log(typeof isStudent); // "boolean"
// 1.4 空值 (Null)
let car = null;
console.log(typeof car); // "object" 注意:这是一个历史遗留问题,typeof null 返回 "object"
// 1.5 未定义 (Undefined)
let undefinedVar;
console.log(typeof undefinedVar); // "undefined"
// 1.6 符号型 (Symbol) - ES6 引入的新类型
let id = Symbol('id');
console.log(typeof id); // "symbol"
// 2. 复杂数据类型 (Complex Types)
// 2.1 对象 (Object)
let person = {
name: "Bob",
age: 30
};
console.log(typeof person); // "object"
// 2.2 数组 (Array) - 是对象的一种特殊形式
let fruits = ["apple", "banana", "orange"];
console.log(typeof fruits); // "object"
console.log(Array.isArray(fruits)); // true
// 2.3 函数 (Function) - 也属于对象的一种
function greet() {
console.log("Hello!");
}
console.log(typeof greet); // "function"
number
、string
、boolean
、null
、undefined
和 symbol
。它们是不可变的,并且直接存储在栈内存中。object
、array
和 function
。它们是引用类型,存储在堆内存中,变量中保存的是对堆内存中实际数据的引用。注意:null
和 function
的 typeof
结果可能与预期不符,null
返回 "object"
,而 function
返回 "function"
。
上一篇:js list 包含
下一篇:js 字符串替换
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站