Home > Web Front-end > JS Tutorial > Things to note when using alert() in JavaScript

Things to note when using alert() in JavaScript

黄舟
Release: 2017-11-20 10:49:08
Original
3255 people have browsed it

In our previous article, we introduced the use of alert() in JavaScript and modified the style of alert(). I believe everyone is familiar with the alert() method and should have used it to reflect Some values ​​in operation. Today I will introduce to you what you need to pay attention to when using alert() in JavaScript! The

alert() method is used to display an alert box with a specified message and an OK button.
Syntax: alert(message)
message represents the plain text (not HTML text) to be displayed in the pop-up dialog box on the window.

alert() is a destructive method. When executed, the code after the pop-up window will not be executed.

Alert() also has the disadvantage that it converts the parameter message into a string.
If we determine that the parameter message is a string when using it, we can use it with confidence, but if not, we should be careful, as follows:

alert([1,2,3]); //弹出的对话框中显示的是1,2,3
Copy after login

In this case, it is still possible Okay, at least we know that the parameter is an array and the content is 1, 2, 3, but the following situation is worse:

<p style="margin-bottom: 7px;">alert([1,2,3,[4,5,6]]); //弹出的对话框中显示的是1,2,3,4,5,6<br/></p>
Copy after login

We can't judge the parameters at all based on what is displayed in the pop-up dialog box It is an array nested within an array. The relationship between the output 1,2,3,4,5,6 and [1,2,3,[4,5,6]] seems to be:

String([1,2,3,[4,5,6]]); //输出的结果是1,2,3,4,5,6
String([1,2,3]); //输出的结果是1,2,3
Copy after login

When using alert(), will the parameter message be first called the String() method to convert the message into a string?

Let’s continue reading:

alert({x:1,y:2});//输出的结果是[object Object]
String(({x:1,y:2}));//输出的结果也是[object Object]
alert("abc");//输出的结果是abcString("abc");//输出的结果是abc
alert(123);//输出的结果是123String(123);//输出的结果是123
typeof(String(({x:1,y:2}))); //输出结果是string
typeof(String([1,2,3,[4,5,6]])); //输出结果是string
typeof(String("abc")); //输出结果是string
typeof(String(123)); //输出结果是string
Copy after login

Summary:

After reading this, we will understand clearly. When alert(), String() will first be called to force the parameter message to a string, and then the string will be displayed in the pop-up dialog box. Therefore, the text displayed in the dialog box popped up by alert() and the parameter message are sometimes different.

Related recommendations:

Introduction to the method of modifying the alert style in JavaScript


Instructions for using alert() in JavaScript


##The difference between alert() and console.log() in javascript

The above is the detailed content of Things to note when using alert() in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template