Home >Web Front-end >JS Tutorial >Learn more about data types in JavaScript
1. Basic data types include:
Number - - (number)
String - - (string)
Boolean - - (Boolean value)
Undefined - - (Undefined)
The following all belong to Object:2.Array - - (array)
3.Function - - (function)
4.Date - - (time)
5.RegExp - - (regular)
6...(and many more)
Number:
Numbers (numbers can be with or without a decimal point), NaN, InfinityString:
String Can be any text within quotes. Both double quotes and single quotes are OK. You can also use the ES6 template string ``, such as:
var a = 'xxx';var a = "xxx";, both of which are reputation strings.
Boolean:
There are only two values: true or false. Boolean values are commonly used in conditional testing. For example, to determine whether 1>2 is correct, return true if it is correct, and return false if it is incorrect. Then we can perform two different operations based on the returned results.Undefined:
Take a value of undefined. means the variable does not contain any value. is an undefined state.Null:
indicates that the value of the variable is empty, and the variable can be cleared by setting the value of the variable to null.Symbol:
For a detailed introduction to Symbol, please go to the detailed introduction of ES6 Ruan Yifeng symbolsArray:
Array: Save a set of data
The function of the array object is : Use separate variable names to store a series of values.
(Dynamic array: the length can be automatically called based on the number of elements)Concept:
1), Element: the data saved in the array space 2), length: the number of elements saved in the array
3), subscript (index): the number of the elements in the array, starting from 0 and ending with (length of the array - 1)
Usage:
var arr = [];//直接量 var arr = new Array();//创建数组对象。 var arr = [1,2,3];//直接量,在创建数组对象的同时初始化保存的数据。 var arr = new Array(1,2,3);在创建数组对象的同时初始化保存的数据。 var arr = new Array(size);//size为数字参数,表示创建数组时先预定size个空间。b. Access array elements:
数组名[下标]c , Array element traversal iteration:
for(let i = 0;i < array.length; i++) { //array[i]}
for(let 变量名 in 数组名) { //变量名中所保存的值是数组下标编号的字符串内容 //仍然使用“数组名[字符串下标]”来访问数组对应下标处的元素}
for(let 变量名 of 数组名) { //变量名中所保存的值是数组中当前便利到的元素值}
Object:
(OOP: Everything is an object) The methods to create objects are: 1. Direct quantity:var stu = { name: '李四', age: 18, eat: function(pig) { console.log('吃:' + pig) }}2. Constructor creation
function Person() { this.name = 'jack'; this.job = function() { alert('program'); }}var person = new Person();3. Create
var person = new Object();person.name = 'jack';person.sex = 'girl';
through object method. Attribute call of object:
Object name.Attribute nameObject name.Method name ([Parameter list])Or: Object name["Attribute name"]Object name["Method name" 】();Function:
A function is an event-driven or reusable block of code when it is called. Essence: code block. Definition: 1), declaration functionfunction 函数名(参数列表) { //函数主体:可被重复使用的代码块}2), function expression
var 变量名 = function(参数列表) { //函数主体:可被重复使用的代码块}3), understanding: new Function() //This is basically no longer needed, the above two are the abbreviations of this.
函数名()b. Event-driven
document.getElementById(‘xx’).onclick = 函数名;parameters (equivalent to the input of a function ):
return 表达式;The function return value is returned to the function calling place
Basic data type The difference from reference data types:
The values of basic data types are stored on the stack. Values of reference data types are stored in the heap. For more programming-related knowledge, please visit:Programming Teaching! !
The above is the detailed content of Learn more about data types in JavaScript. For more information, please follow other related articles on the PHP Chinese website!