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.
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.
Tip: 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.
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:
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.
JavaScript comments
JavaScript does not execute annotations.
We can add comments to explain JavaScript, or to improve the readability of the code.
Single-line comments begin with//.
JavaScript Multi-line comments
Multiline comments start with/* and end with */.
JavaScript variables
Like algebra, JavaScript variables can be used to hold values (such as x=2) and expressions (such as z=x+y).
Variables can use short names (such as x and y), or they can use better descriptive names (such as age, Sum, totalvolume).
- Variables must start with a letter
- Variables can also start with the $ and _ symbols (although we do not recommend this)
- Variable names are case sensitive (Y and y are different variables)
Tip: Both JavaScript statements and JavaScript variables are case-sensitive.
JavaScript Data types
JavaScript variables can also hold other data types, such as text values (name= "Bill Gates").
In JavaScript, a text like "Bill Gates" is called a string.
There are many types of JavaScript variables, but for now, we only focus on numbers and strings.
When you assign a text value to a variable, enclose the value in double or single quotation marks.
Do not use quotation marks when the value you assign to a variable is numeric. If you enclose a value in quotation marks, the value is treated as text.
JavaScript Timing Events
By using JavaScript, we have the ability to execute code after a set interval of time, not immediately after the function is called. We call this a timing event.
It is easy to use timing events in JAVASCRITP, and two key methods are:
-
SetTimeout ()
-
Future execution of code at some time
-
Cleartimeout ()
-
Cancel settimeout ()
JavaScript Basics----Basic syntax