JavaScript statements
JavaScript statement sends a command to the browser. The purpose of the statement is to tell the browser what to do.
The following JavaScript statement outputs the text "Hello World" to the HTML element with id="demo":
document.getElementById("demo").innerHTML="Hello World" ;
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>PHP中文网(php.cn)</title> </head> <body> <h1>我的网页</h1> <p id="demo">段落。</p> <script> document.getElementById("demo").innerHTML="Hello World"; </script> </body> </html>
Run the code and try it
points Symbol;
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.
Tip: 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中文网(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 the program and try it
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中文网(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 the program and try it
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):
Statement | Description |
---|---|
break | is used to break out of the loop. |
catch | Statement block, execute the catch statement block when an error occurs during the try statement block execution. |
continue | Skips an iteration in the 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, the code block can be executed a specified number of times. |
for ... in | is used to traverse the properties of an array or object (loop through the properties of an array or object). |
function | Define a function |
if ... else | is used based on different conditions to perform different actions. |
return | Exit function |
switch | is used to execute different functions based on different conditions action. |
throw | Throw (generate) error. |
try | Implement error handling and use it with catch. |
var | Declare a variable. |
while | When the conditional statement is true, execute the statement block. |
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";
Wrap lines of code
You can Use backslashes 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!");
Tips
JavaScript is a scripting language. The browser will execute the script code line by line as it reads the code. With traditional programming, all code is compiled before execution.