JavaScript (2): variables, data types, type conversion

1. The use of variables

The essence of variables: a variable is a space that a program applies for in memory to store data

1. Declare variables

var variable name;

For example:

var=age;
2. Assignment

variable name = value

For example:

age=10;//给age这个变量赋值为10

insert image description here

3. Variable initialization

Declaring a variable and assigning a value is called variable initialization
For example:

var age=18;//声明变量同时赋值为18
4. Variable update
  • After a variable is reassigned, its original value will be overwritten, and the variable value will be based on the last assigned value

For example:

insert image description here

Although updated, the original variable value will still be displayed

5. Declare multiple variables at the same time

When declaring multiple variables at the same time, you only need to write one var, and use English commas to separate multiple variable names.
For example:

var age=10,name='sy',sex=0;
6. Special cases for declaring variables

Please add a picture description

7. Variable naming rules

Please add a picture description

2. Data type

Please add a picture description

digital

1. Digital Number

Please add a picture description

Browsers convert to decimal by default

2. Digital range

JavaScript heavy value maximum and minimum values
Please add a picture description

3. Numeric special values

alert(Infinity);//Infinity,代表无穷大,大于任何值
alert(-Infinity);//-Infinity,代表无穷小,小于任何值
alert(NaN);//NaN,not a number,代表是一个非数值

4.isNaA()

Used to determine whether a variable is a non-numeric type, return true or false

  • If it is a number, return false
  • Returns true if not a number
    Please add a picture description

string type String

1. String format

Please add a picture description

2. Nesting of string quotes

JS can nest double quotes with single quotes, or nest single quotes with double quotes (outer double inner single, outer single inner double)
for example:
Please add a picture description

3. String escape character

Please add a picture description

For example:

Please add a picture description

4. String length

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

For example:

var str= 'hello,world!';
alert(str.length);//结果显示为12

5. String concatenation

  • Multiple strings can be spliced ​​using +, and the splicing method is string + any type = new string after splicing
  • Before splicing, any type added to the string will be converted to a string, and spliced ​​into a new string

For example:
Please add a picture description

The formula for the plus sign is: adding values ​​and connecting characters

Boolean Boolean

Please add a picture description

Undefined and Null

Please add a picture description

Get the data type of the detection variable

Use typeof to get the data type of the detected variable

For example:
Please add a picture description

The number entered by the prompt is also a string type, which is the same as python's input

3. Data type conversion

1. Convert to string

Please add a picture description
For example:
Please add a picture description

2. Convert to digital

Please add a picture description

3. Convert to Boolean

Please add a picture description

Guess you like

Origin blog.csdn.net/weixin_53912712/article/details/128279080