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

lamp# 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%">

##break is used to break out of the loop. Statement block, execute the catch statement block when an error occurs during the try statement block Skip the loop. An iteration. ##do ... whileExecute a block of statements and continue executing the block if the conditional statement is true. ##forfor ... in##functionDefine a function#. Used to perform different actions based on different conditions ##returnExit function#. ##tryImplements error handling and is used with catch. ##whileWhen the conditional statement is true, execute the statement 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";



Wrap lines of code

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

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

However, you cannot wrap lines like this :

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



Did you know ?

Tip: 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.


StatementDescription
#catch
continue
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)
##if ... else
##switch is used to perform different actions based on different conditions
throwThrow. Output (generate) errors.
Declare a variable.