Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JavaScript statement


May 06, 2021 JavaScript


Table of contents


JavaScript statement


The command issued by the JavaScript statement to the browser. The function of a statement is to tell the browser what to do.


JavaScript statement

The JavaScript statement is a command sent to the browser.

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

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

document.getElementById( "demo" ).innerHTML = "Hello Dolly." ;

Try it out . . .


A sign;

A sign is used to separate JavaScript statements.

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

Another use in using a half sign is to write multiple statements in a row.

Writing:

a = 5 ;
b = 6 ;
c = a + b;

Is the same as writing:

a = 5 ; b = 6 ; c = a + b;

Try it out . . .
JavaScript statement You may also see cases without a sign.
In JavaScript, it is optional to end a statement with a sign.


JavaScript code

JavaScript code is a sequence of JavaScript statements.

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

This example outputs a title and two paragraphs to a Web page:

Document.GetElementByid ("demo"). Innerhtml = "Hello Dolly";
Document.GetElementByid ("MyDiv"). InnerHtml = "How are you doing recently?"

Try it out . . .


JavaScript block of code

JavaScript can be combined in batches.

The block of code starts with an opening brace and ends with a closing brace.

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

This example outputs a title and two paragraphs to a Web page:

function myFunction()
{
Document.GetElementByid ("demo"). Innerhtml = "Hello Dolly";
Document.GetElementByid ("MyDiv"). InnerHtml = "How are you doing recently?"
}

Try it out . . .

You'll learn more about functions in a later section.


JavaScript statement identifier

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

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

The following table lists JavaScript statement identifiers (keywords):

Statement Describe
break Used to jump out of a loop.
Catch Statement block, which executes catch statement blocks when an error is made in the execution of the try statement block.
continue Skip an iteration in the loop.
do ... while Executes a statement block that continues when the conditional statement is true.
for When the conditional statement is true, you can execute the block of code a specified number of times.
for ... in A property used to traverse an array or object (looping through the properties of an array or object).
function Define a function
if ... else Used to perform different actions based on different conditions.
return Exit the function
switch Used to perform different actions based on different conditions.
throw Throw (build) an error.
try Implement error handling, which is used with catch.
Var Declare a variable.
while When the conditional statement is true, the statement block is executed.



Space

JavaScript ignores extra spaces. Y ou can add spaces to your script to improve its readability. The following two lines of code are equivalent:

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

Fold the line of code

You can use backslashes in text strings to line lines of code. The following example shows correctly:

Document.write ("Hello \ w3cschool!");

However, you can't fold a line like this:

Document.write \ ("Hello W3cschool!");

JavaScript statement practice

JavaScript comment statement action

Practice commenting statements in JavaScript, and the commented blocks of code will not be run in JavaScript.