JavaScript typeof, null, and undefined, JavaScript type conversions, regular expressions, wrong - throw, try and catch, debugging, variable lift

1, typeof operator

You can use the typeof operator to detect the data type of the variable.
Here Insert Picture Description

2、null

In JavaScript null means "nothing."

null is only a special type of a value. It represents a null object reference.
Here Insert Picture Description

3、undefined

In JavaScript, undefined variable is not set a value.

Typeof a variable without a value returns undefined.
Here Insert Picture Description

4, JavaScript type conversions

4.1, JavaScript Data Types

There are five different types of data in JavaScript:

string
number
boolean
object
function

Three kinds of object types:

Object
Date
Array

2 does not contain any type of data values:

null
undefined

4.2, typeof operator

You can use the typeof operator to view the data type of JavaScript variable.
Here Insert Picture Description

NaN 的数据类型是 number
数组(Array)的数据类型是 object
日期(Date)的数据类型为 object
null 的数据类型是 object
未定义变量的数据类型为 undefined

4.3, constructor property

constructor constructor property returns all JavaScript variables.
Here Insert Picture Description

4.4, JavaScript type conversions

JavaScript variables can be converted to the new variables or other data types:

通过使用 JavaScript 函数
通过 JavaScript 自身自动转换

4.5, the digital conversion to a string

Global method String () can be converted to a string number.
Here Insert Picture Description

4.6, Number Method toString () also has the same effect.

Here Insert Picture Description

5, JavaScript regular expressions

Search mode may be used to replace text search and text.

5.1, Syntax

/ Regular expression subject / modifier (optional)

Where modifier is optional.
Here Insert Picture Description

5.2, using the method of the string

In JavaScript, the regular expression method commonly used for two strings: search () and replace ().

replace () method for replacing some characters other characters, or alternatively a substring match the positive expression in using string.

5.3, search () method uses a regular expression

search () method for substring search character string specified, retrieving or regular expression matching substring, and return to the starting position of the substring.
Here Insert Picture Description

5.4、search() 方法使用字符串

search 方法可使用字符串作为参数。字符串参数会转换为正则表达式:
Here Insert Picture Description

5.5、replace() 方法使用正则表达式Here Insert Picture Description

5.6、正则表达式修饰符

Here Insert Picture Description

5.7、正则表达式模式

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

5.8、使用 RegExp 对象

在 JavaScript 中,RegExp 对象是一个预定义了属性和方法的正则表达式对象。

5.9、使用 test()

test() 方法是一个正则表达式方法。

test() 方法用于检测一个字符串是否匹配某个模式,如果字符串中含有匹配的文本,则返回 true,否则返回 false。
Here Insert Picture Description

5.10\使用 exec()

exec() 方法是一个正则表达式方法。

exec() 方法用于检索字符串中的正则表达式的匹配。

该函数返回一个数组,其中存放匹配的结果。如果未找到匹配,则返回值为 null。

以下实例用于搜索字符串中的字母 “e”:
Here Insert Picture Description

6、JavaScript 错误 - throw、try 和 catch

try 语句测试代码块的错误。

catch 语句处理错误。

throw 语句创建自定义错误。

finally 语句在 try 和 catch 语句之后,无论是否有触发异常,该语句都会执行。

6.1、JavaScript try 和 catch

try 语句允许我们定义在执行时进行错误测试的代码块。

catch 语句允许我们定义当 try 代码块发生错误时,所执行的代码块。

JavaScript 语句 try 和 catch 是成对出现的。
Here Insert Picture Description
实例

在下面的例子中,我们故意在 try 块的代码中写了一个错字。

catch 块会捕捉到 try 块中的错误,并执行代码来处理它。
Here Insert Picture Description

6.2、finally 语句

finally 语句不论之前的 try 和 catch 中是否产生异常都会执行该代码块。
Here Insert Picture Description

6.3、Throw 语句

throw 语句允许我们创建自定义错误。

正确的技术术语是:创建或抛出异常(exception)。

If used with a try to throw and catch, you can control program flow and generate a custom error messages.
Here Insert Picture Description

7, JavaScript debugging

7.1, JavaScript debugging tool

With debugging tools, we can set a breakpoint (stop position of code execution), and can detect variables in the code execution.

Enabled browser debugging tools in general is to press the F12 key, and select Debug menu "Console".
console.log () method

If the browser supports debugging, you can use console.log () method printing values ​​on JavaScript debug window:
Here Insert Picture Description

7.2, set breakpoints

In the debug window, you can set breakpoints JavaScript code.

On each breakpoint will stop execute JavaScript code to check the value of the JavaScript variable to us.

After the check is completed, you can re-execute the code (e.g., a play button).

7.3, debugger keyword

debugger keyword to stop execution of JavaScript, and invoke the debugger functions.
Open debugger, code execution has stopped in front of the third row.
Here Insert Picture Description

8, JavaScript variable lift

JavaScript, functions and variable declaration will be promoted to the top most function.

JavaScript, variables can be declared after use, that is, variables can be declared before use again.

The following two examples will get the same result:
Here Insert Picture Description
Here Insert Picture Description

8.1, JavaScript will not initialize upgrade

JavaScript variable declarations will only increase, not initialized.
Here Insert Picture Description
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_43731793/article/details/92400078