JavaScript stat...LOGIN

JavaScript statements

JavaScript statement

JavaScript statement sends a command to the browser. The purpose of a statement is to tell the browser what to do.

JavaScript Statement

JavaScript statement is a command sent to the browser.

The purpose of these commands is to tell the browser what to do.

The following JavaScript statement outputs the text "Hello Dolly" to the HTML element with id="demo":

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
</head>
<body>
<h1>My web page</h1>
<p id="demo">This is a paragraph</p>
<script>
document.getElementById("demo").innerHTML = "你好 Dolly";
</script>
</body>
</html>

Semicolon;

Semicolons are used to separate JavaScript statements.

Usually we add a semicolon at the end of each executable statement.

Another use of semicolons is to write multiple statements on one line.

Tips: You may also see cases without semicolons.
In JavaScript, it is optional to end a statement with a semicolon.

JavaScript Code

JavaScript code is a sequence of JavaScript statements.

The browser executes each statement in sequence in the order in which it was written.

This example outputs a title and two paragraphs to the web page:

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8">  
</head>
<body>
<h1>我的 Web 页面</h1>
<p id="demo">一个段落。</p>
<div id="myDIV">一个 DIV。</div>
<script>
document.getElementById("demo").innerHTML="你好 Dolly";
document.getElementById("myDIV").innerHTML="你最近怎么样?";
</script>
</body>
</html>

JavaScript code block

JavaScript can be batched combined.

The code block starts with a left curly brace and ends with a right curly brace.

The function of a code block is to execute a sequence of statements together.

This example outputs a title and two paragraphs to the web page:

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8">  
</head>
<body>
<h1>我的 Web 页面</h1>
<p id="myPar">我是一个段落。</p>
<div id="myDiv">我是一个div。</div>
<p>
<button type="button" onclick="myFunction()">点击这里</button>
</p>
<script>
function myFunction(){
document.getElementById("myPar").innerHTML="你好世界!";
document.getElementById("myDiv").innerHTML="你最近怎么样?";
}
</script>
<p>当您点击上面的按钮时,两个元素会改变。</p>
</body>
</html>

JavaScript statement identifier

JavaScript statements usually start with a statement identifier and execute the statement.

Statement identifiers are reserved keywords and cannot be used as variable names.

The following table lists JavaScript statement identifiers (keywords):

<table class="reference" "style="width: 100%" style="border: 0px; margin: 4px 0px; padding: 0px; width: 729px; color: rgb(51, 51, 51); font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, STHeiti, "Microsoft Yahei", sans -serif; font-size: 12px; white-space: normal; background-color: rgb(255, 255, 255);">


#Statement                                           Description


#break                                                                                                                                           to break out of the loop. Skip An iteration in a loop.

do ... while Execute a statement block and continue executing the statement block when the conditional statement is true.

for When the conditional statement is true, you can The code block is executed a specified number of times.

for ... in is used to iterate over the properties of the array or object (loop through the properties of the array or object)

#if ... else                                                                                                                                                    ​to perform different actions #.

##throw Throw (generate) error.

try                                                                                                                                                                                                                                                                                                  ​

var                                                                                                                                   to declare a variable.

While When the conditional statement is true, execute the sentence block.

JavaScript is case-sensitive.

JavaScript is case-sensitive.

When writing JavaScript statements, please pay attention to whether the case switching key is turned off.

The functions getElementById and getElementbyID are different.

Similarly, variables myVariable and MyVariable are also different.

Spaces

JavaScript will ignore extra spaces. You can add spaces to your script to make it more readable. The following two lines of code are equivalent:

var person="Hege";
var person = "Hege";


Correct code Line Wrap

You can use backslashes in text strings to wrap lines of code. The following example will display correctly:

document.write("Hello\
world!");

However, you cannot wrap lines like this:

document.write \
("Hello world!");


Next Section
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <h1>My web page</h1> <p id="demo">This is a paragraph</p> <script> document.getElementById("demo").innerHTML = "你好 Dolly"; </script> </body> </html>
submitReset Code
ChapterCourseware