JavaScript statements
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":
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <h1>我的网页</h1> <p id="demo">我的第一个段落。</p> <script> document.getElementById("demo").innerHTML = "你好 Dolly"; </script> </body> </html>
Run Example»
Click the "Run Example" button to view the online example
Semicolon;
The semicolon is 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.
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <h1>我的网页</h1> <p id="demo1"></p> <p id="demo2"></p> <script> a = 1; b = 2; c = a + b; document.getElementById("demo1").innerHTML = c; x = 1; y = 2; z = x + y; document.getElementById("demo2").innerHTML = z; </script> </body> </html>
Run Instance»
Click the "Run Instance" button to view the online instance
# You may also see cases without semicolons. In JavaScript, terminating a statement with a semicolon is optional. |
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:
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </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>
Run Example»
Click the "Run Example" button to view the online example
JavaScript Code Blocks
JavaScript can be combined in batches.
A 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:
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </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>
Run Example»
Click the "Run Example" button to view the online example
You will learn more about functions in later chapters.
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%">
Statement | Description |
---|---|
is used to break out of the loop. | #catch |
continue | |
##do ... while | Execute a block of statements and continue executing the block if the conditional statement is true. |
When the conditional statement is true, the code block can be executed a specified number of times | |
Used to traverse the properties of an array or object (loop through the properties of an array or object) | ##function |
#. | ##if ... else |
##return | Exit function#. |
##switch | is used to perform different actions based on different conditions |
throw | Throw. Output (generate) errors. |
Implements error handling and is used with catch. | Declare a variable. |
When the conditional statement is true, execute the statement block. |