6. JavaScript data types

foreword

The role of this video teaching is limited to students who need to get started with 0 basics. The blogger will plan other courses in the later stage. If you do
n’t understand the video or have any questions, please contact me directly. I will answer
the courseware information. You can join the self-study study group, learn with your peers, and communicate more conveniently
vx search public account [Front-end New Weather] If you have my WeChat, please be sure to fill in the remarks (B station-name)

Courseware code address https://github.com/haojiey/js-Introductory-courseware

what can be learned

  • What are the js data types
  • Various types of usage

type of data

Data type refers to the type of value that can be stored and manipulated in the program. Different data types are used to store different data, such as text and numeric values.
The data types in JavaScript can be divided into two types:
basic data types : string (String), number (Number), Boolean (Boolean), empty (Null), undefined (Undefined), Symbol.
Reference data type : object (Object), array (Array), function (Function)
Note: Symbol is a new data type introduced in ECMAScript6, which represents a unique value.

typeof

Used to detect the data type of the variable

typeof "1"                // 返回 string
typeof 1                 // 返回 number
typeof false                 // 返回 boolean
typeof []             // 返回 object
typeof {
    
    } // 返回 object

basic data type

String string

The variable used to store the text class is any text in quotation marks, you can use single quotation marks or double quotation marks

var name="my name is xiaomu";
var name="hellow";
var name='hellow'

Number

It is a frequently used number, including integers, decimals (floating point numbers)

var num = 99;
var phoneNumber = 1830011133
var x = 0.23;
var y = 1.00;

Boolean Boolean

Boolean type means correct and wrong, only two values ​​true means correct, false means error, and can also be defined by expressions

var x=true;
var y=false;

// 表达式
var q = 1 < 2;
var s = 1 > 2;

Null empty

Null is a special data type with only one value, representing an "empty" value, that is, there is no value, nothing, used to define a null object pointer, and the variable can be emptied by setting the value of the variable to
null

var x=true;

x = null;

Undefined undefined

Undefined is also a special data type with only one value, representing undefined. When we declare a variable but do not assign a value to the variable, the default value of this variable is Undefined.

var name;
console.log(name);
// 输出 undefined

Symbol is unique

Symbol is a new data type introduced in ECMAScript6, which represents a unique value, and the value of Symbol type needs to be generated using the Symbol() function

var str = "123";
var sym1 = Symbol(str);
var sym2 = Symbol(str);
console.log(sym1);          // 输出 Symbol(123)
console.log(sym2);          // 输出 Symbol(123)
console.log(sym1 == sym2);  // 输出 false :虽然 sym1 与 sym2 看起来是相同的,但实际上它们并不一样,根据 Symbol 类型的特点,sym1 和 sym2 都是独一无二的

reference data type

Object object

Objects are defined by curly braces, inside which the properties of the object are defined in the form of name and value pairs (name : value). Properties are separated by commas:

var person={
    
    
  firstname:"John",
  lastname:"Doe",
  id:5566
};

The keys of the object type are all string types, and the values ​​can be of any data type. To get a value in an object, you can use the form objectname.key or objectname['key']

name=person.lastname;
name=person["lastname"];
console.log(name) //输出结果Doe

Array array

It is a collection of data arranged in order. Each value in the array is called an element, and the array can contain any type of data. To define an array in JavaScript, you need to use square brackets [ ], and each element in the array is separated by a comma, for example:

var cars=["Saab","Volvo","BMW"];

or create as a function

var arr = new Array(1, 2, 3, 4);
console.log(arr);       // 输出 [1, 2, 3, 4]

Get a single element
accessed by index. The index in the array starts from 0 and increases sequentially, that is to say, the index of the first element of the array is 0, the index of the second element is 1, and the index of the third element is 2.

var arr = [1, 2, 3, 'Hello', null, false];
console.log(arr[0]);  // 输出索引为 0 的元素,即 1
console.log(arr[5]);  // 输出索引为 5 的元素,即 false
console.log(arr[6]);  // 索引超出了范围,返回 undefined

Function function

It is a block of code with a specific function. The function does not run automatically. It needs to be called by the function name to run.

function hi(){
    
    
  return "Hello";
}
var res = hi();
//console.log(hi());
console.log(res);  // 输出 Hello

Functions can also pass parameters

function hi(name){
    
    
  return "Hello" + name;
}
var res = hi('xiaomu');
//console.log(hi());
console.log(res);  // 输出 Hello xiaomu

Guess you like

Origin blog.csdn.net/weixin_55123102/article/details/130255732