[Javascript study notes] those things about simple data types (all details, I have bookmarked)

Variable data type

Variables are used to store values, they have names and data types. The data type of the variable determines how the bits representing these values ​​are stored in the computer's memory. JavaScript is a weakly typed or master dynamic language. this meansNo need to declare variable types in advance, In the process of running the program, the type will be automatically determined.

JS does not need to declare the type

	var age=10;//这是一个数字型
	var areyouok='是的';//这是一个字符串

The data type of JS can be changed

//js是动态语言 变量的数据类型是可以变化的
	var x=10;
	X='41';

Classification of data types

Simple data type

Simple data type Description Defaults
Number Number type, including integer value and floating point value, such as 21, 0.21 0
Boolean Boolean value type, such as true, false, equivalent to 1, 0 false
String String type, such as'Zhang San'. Note that in our js, strings are all quoted ”“
Undefined var a; variable a is declared but no value is given, at this time a=undefined undefined
Null var a=null; declares that the variable a is null null

Number

Numerical base

In JS, add 0 in front of octal, and add 0x in front of hexadecimal

        //1.八进制 0-7
        var num1=010;
        console.log(num1);//010 八进制 转换为10进制就是8
        var num2=012;
        console.log(num2);
        //2.十六进制 0-9 a-f #fff
        var num3=0x9;
        console.log(num3);
        var num4=0xa;
        console.log(num4);
Digital range
 		console.log(Number.MAX_VALUE);//1.7976931348623157e+308
        console.log(Number.MIN_VALUE);//5e-324
Three special numerical values
        console.log(Infinity);//代表无穷大,大于任何数值
        console.log(-Infinity);//代表无穷小,小于任何数值
        console.log(NaN);//代表一个非数值

isNaN() uses this method to judge non-number, if it is a number, it returns false, otherwise it returns true

String type String

The string type can be any text in quotation marks, and its syntax is == "double quotation mark"with"Single quotation mark"==

The attributes in the html tag use double quotation marks, we recommend using JSapostrophe

String quotation marks nested

JS can use single quotes to nest double quotes, or use double quotes to nest single quotes == (outer double inner but, outer single inner double) ==

String escape character

Similar to the special characters in HTML, there are special characters in strings, which we call escape characters.
The escape characters all start with \. The commonly used escape characters and their descriptions are as follows:

Escapes explain
\n Newline character, n means newline
\ Slash\
'apostrophe
" "Double quotes
\t tab indentation
\b Space, b means blank
String length

A string is composed of several characters, and the number of these characters is the length of the string. The length of the entire string can be obtained through the length property of the string.

        var str='my name is 41';
        console.log(str.length);
String splicing

Here is a simple interactive programming, enter your age, and then output your age after string splicing

        var age=prompt('input your age:')
        var str='your age is '+age;
        alert(str);

The splicing method is String + any type = string after splicing

Boolean

Complex data types (introduced later in this blog)

Get variable data type

typeof can be used to detect the data type of a variable

        var num=10;
        console.log(typeof num);

The value taken by prompt is a character type!

Data type conversion

Convert to string

the way Description Case study
toString() Into a string var num = 1; alert (num.toString ());
String() coercion Into a string var num = 1; alert (String (num));
Plus sign concatenation string The result of splicing with the string is a string var num=1;alert(num+"");

Convert to digital type (emphasis)

the way Description Case study
parseInt(string) function Convert string type to integer numeric type parseInt(’78‘)
parseFloat(string) function Convert string type to floating-point numeric type parseFloat(’78.21‘)
Number() casting function Convert string type to numeric type Number(‘12’)
js implicit conversion (. * /) Use arithmetic operations to implicitly convert to numeric ’12‘-0

Note that I and F are capitalized

Calculate the age case

        var year=prompt('请输入您的出生年份:');
        var age=2021-year;
        alert('your age is '+age);

Convert to Boolean

the way Description Case study
Boolean() function Convert other types to boolean Boolean(’true‘);

representativeEmpty, negative valueWill be converted to false, such as 0, NaN, null, undefined and
other values ​​will be converted to true

Guess you like

Origin blog.csdn.net/qq_42136832/article/details/115213816