JavaScript statements
A command issued by a JavaScript statement 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 of the Id= "demo":
document.getElementById ("Demo"). Innerhtml= "Hello World";
Semicolon
Semicolons are used to separate JavaScript statements.
Usually we add semicolons at the end of each executable statement.
Another useful use of semicolons is to write multiple statements in one line.
Tips: You may also see cases with no semicolons.
In JavaScript, concluding sentences with semicolons are optional.
JavaScript Code
JavaScript code (or JavaScript only) is a sequence of javascript statements.
The browser executes each statement in the order in which it is written.
This example will manipulate two HTML elements:
Instance:
<!DOCTYPE HTML><HTML><Body><H1>My Web Page</H1><PID= "Demo">A Paragraph.</P><DivID= "Mydiv">A DIV.</Div><Script>document.getElementById ("Demo"). InnerHTML="Hello World";d Ocument.getelementbyid ("mydiv"). InnerHTML="How is it ?";</Script></Body></HTML>
Effect:
My Web Page
Hello World
How is it?
JavaScript code block
JavaScript statements are combined in the form of blocks of code.
The block starts with the left curly brace and ends with the closing curly brace.
The function of a block is to make the statement sequence execute together.
JavaScript functions are a typical example of combining statements in blocks.
The following example runs a function that can manipulate two HTML elements:
Instance:
<!DOCTYPE HTML><HTML><Body><H1>My Web Page</H1><PID= "Mypar">I am a paragraph.</P><DivID= "Mydiv">I am a div.</Div><P><Buttontype= "button"onclick= "myFunction ()">Click here</Button></P><Script>functionmyFunction () {document.getElementById ("Mypar"). InnerHTML="Hello World";d Ocument.getelementbyid ("mydiv"). InnerHTML="How is it ?";}</Script><P>When you click on the button above, two elements will change.</P></Body></HTML>
Effect:
My Web Page
I am a paragraph.
I am a div.
Click here
When you click on the button above, two elements will change.
You'll learn more about functions in a later chapter.
JavaScript is case sensitive.
JavaScript is sensitive to capitalization.
When writing JavaScript statements, be aware of whether to turn off the case toggle key.
The function getElementById is different from the getElementById.
Similarly, variable myvariable and myvariable are also different.
Space
JavaScript ignores extra spaces. You can add spaces to the script to improve its readability. The following two lines of code are equivalent:
var name= "Hello";
var name = "Hello";
Wrapping lines of code
You can wrap lines of code in a text string with backslashes. The following example will display correctly:
document.write ("Hello \
World! ");
However, you cannot fold a line like this:
document.write \
("Hello world!");
Did you know that?
Tips: JavaScript is a scripting language. The browser executes the script code on a line-by-row basis when reading the code. For traditional programming, all code is compiled before execution.
Front-end JavaScript next day study (4)-javascript-statement