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

    PHP中文网(php.cn) 

我的网页

段落。

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

    PHP中文网(php.cn) 

我的 Web 页面

一个段落。

一个 DIV。

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

    PHP中文网(php.cn) 

我的 Web 页面

我是一个段落。

我是一个div。

当您点击上面的按钮时,两个元素会改变。

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.



Continuing Learning
||
PHP中文网(php.cn)

我的网页

段落。

submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!