[JavaScript tutorial] JavaScript statements

Source: Internet
Author: User
JavaScript statement to the browser. The statement is used to tell the browser what to do.

JavaScript statements

JavaScript statement to the browser. The statement is used to tell the browser what to do.

JavaScript statements

The JavaScript statement is a command sent to the browser.

These commands are used to tell the browser what to do.

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

Instance

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

Semicolon;

A semicolon is used to separate JavaScript statements.

We usually add a semicolon at the end of each executable statement.

Another use of semicolons is to write multiple statements in one line.

Writing:

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

Is the same as writing:

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

You may also see cases without semicolons.
In JavaScript, it is optional to use a semicolon to conclude a sentence.

JavaScript code

JavaScript code is a sequence of JavaScript statements.

The Browser executes each statement in order of writing.

In this example, a title and two paragraphs are output to the webpage:

Instance

Document. getElementById ("demo"). innerHTML = "Hello Dolly"; document. getElementById ("myDIV"). innerHTML = "How are you doing recently? ";

JavaScript code block

JavaScript can be combined in batches.

The code block starts with the left curly braces and ends with the right curly braces.

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

In this example, a title and two paragraphs are output to the webpage:

Instance

Function myFunction () {document. getElementById ("demo"). innerHTML = "dododolly"; document. getElementById ("myDIV"). innerHTML = "How are you doing recently? ";}

You will learn more about functions in a later chapter.

JavaScript statement identifier

A JavaScript statement usually starts with a statement identifier and runs the statement.

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

The following table lists the JavaScript statement Identifiers (keywords ):


Statement

Description


Break is used to jump out of a loop.

Catch statement block. When an error occurs during execution of the try statement block, the catch statement block is executed.

Continue skips an iteration in the loop.

Do... while executes a statement block and continues to execute this block when the condition statement is true.

When the condition statement is true, you can run the code block for a specified number of times.

For... in is used to traverse the attributes of an array or object (cyclic operation on the attributes of an array or object ).

Function defines a function.

If... else is used to execute different actions based on different conditions.

Return exit function

A switch is used to execute different actions based on different conditions.

Throw (generated) error.

Try implements error handling and is used together with catch.

Var declares a variable.

While when the condition statement is true, execute the statement block.

JavaScript is case sensitive.

JavaScript is case sensitive.

When writing JavaScript statements, check whether the case-sensitivity switch is disabled.

The getElementById and getElementbyID functions are different.

Similarly, the variables myVariable and MyVariable are different.

Space

JavaScript ignores unnecessary spaces. You can add spaces to the script to improve readability. The following two lines of code are equivalent:


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

Lines of code

You can use a backslash to wrap a line in a text string. The following example is displayed correctly:

Document. write ("Hello, world! ");

However, you cannot fold like this:

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

Do you know?

Tip: JavaScript is a scripting language. When the browser reads the code, it executes the script code line by line. For traditional programming, all code is compiled before execution.

The above is the content of the [JavaScript tutorial] JavaScript statement. For more information, see the PHP Chinese website (www.php1.cn )!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.