A command issued by a JavaScript statement to a browser. The statement tells the browser what to do and JavaScript comments can be used to make your code more readable, right

JavaScript statements

JavaScript statements are commands sent to the browser that tell the browser what to do. The following JavaScript statement outputs the text “hello Dolly” to the HTML element with id=”demo” :

<! DOCTYPEhtml>
<html>
  <body>
    <h1>My web page</h1>
    <p id="demo">My first paragraph</p>

    <script>
      document.getElementById("demo").innerHTML = "Hello Dolly";
    </script>
  </body>
</html>
Copy the code

Try it »

A semicolon.

Semicolons are used to separate JavaScript statements, and usually we add semicolons to the end of each executable statement. Another use for semicolons is to write multiple statements in a single line

<! DOCTYPEhtml>
<html>
  <body>

    <h1>My web page</h1>
    <p id="demo1"></p>
    <p id="demo2"></p>

    <script>
      x = 1; y = 2; z = x + y
      document.getElementById("demo1").innerHTML = z
      
      a = 10
      b = 20
      c = a + b
      document.getElementById("demo2").innerHTML = c
    </script>

  </body>
</html>
Copy the code

In JavaScript, it is optional to end a statement with a semicolon; the trailing semicolon at the end of a line can be omitted.Try it »





JavaScript code

JavaScript code is a sequence of JavaScript statements, and the browser executes each statement in the order it was written. In this example, it prints a title and two paragraphs to the web page:

<! DOCTYPEhtml>
<html>
  <body>

    <h1>My Web page</h1>
    <p id="demo">One paragraph.</p>
    <div id="myDIV">A DIV.</div>
    <script>
      document.getElementById("demo").innerHTML = "Hello Dolly";
      document.getElementById("myDIV").innerHTML = "How are you doing?";
    </script>

  </body>
</html>
Copy the code

Try it »

JavaScript code block

JavaScript can be grouped in batches, with blocks of code starting with open curly braces and ending with close curly braces, to execute a sequence of statements together. In this example, a heading and two paragraphs are output to a web page:

<! DOCTYPEhtml>
<html>
  <body>

    <h1>My Web page</h1>
    <p id="myPar">I am a paragraph.</p>
    <div id="myDiv">I'm a div.</div>
    <p>
      <button type="button" onclick="myFunction()">Click here to</button>
    </p>
    <p>When you click the button above, the two elements change</p>
    
    <script>
      function myFunction() {
        document.getElementById("myPar").innerHTML = "Hello world!;
        document.getElementById("myDiv").innerHTML = "How are you doing?";
      }
    </script>

  </body>
</html>
Copy the code

Try it »

JavaScript statement identifier

JavaScript statements typically start with a statement identifier and execute that statement. Statement identifiers are reserved keywords that cannot be used as variable names. The following table lists JavaScript statement identifiers (keywords) :

statements describe
break Used to break out of a loop
catch Statement block, which executes a catch block in the event of a try block execution error
continue Skip an iteration in the loop
do … while Executes a block of statements and continues to execute the block if the conditional statement is true
for When the conditional statement is true, a code block can be executed a specified number of times
for … in Use to iterate over arrays or object properties (loop over arrays or object properties)

for (var key in object) { }
for … of Properties and values used to iterate over iterators

The array Object. Entries ({name: “Ken”})

for (var [k,v] of object) { }
function Define a function
if … else Used to perform different actions based on different conditions
return Exit function
switch Used to perform different actions based on different conditions
throw Throw (build) an error
try Implement error handling, used with catch
var Declare a variable
while When the conditional statement is true, the statement block is executed


The blank space

JavaScript ignores extra whitespace. You can add whitespace to the script to improve its readability. The following two lines are equivalent:

var person="Hege";
var person = "Hege";
Copy the code


Folds lines of code

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

document.write("Hello world!");
Copy the code

However, you can’t fold lines like this:

document.write \ 
("Hello world!);
Copy the code


JavaScript comments

You can add comments to explain JavaScript or to improve the readability of your code. Single-line comments start with //, multi-line comments start with /*, and end with */ This example uses a single-line comment to explain the code:

// Output title:
document.getElementById("myH1").innerHTML = "Welcome to my homepage."

// Comments at the end of the line
var x=5;    // Declare x and assign 5 to it
var y=x+2;  // Declare y and assign x+2 to it

/* Multiline comment 1 */

/* Multiline comment 2 document.getelementById ("myP").innerhtml = "This is my first paragraph." * /


//Automatically generates detailed JSDoc comments in TypeScript and JavaScript files
/ * * *@description
 * @author A breeze *@param {*} Params1 Parameter 1. The number type is *@param {*} Params2 Parameter 2. The value is a string of characters *@returns The number type is */
function methods(params1, params2) {
  return 123
}

methods()
Copy the code








JavaScript keywords

JavaScript keywords are used to identify actions to be performed, and like any other programming language, JavaScript reserves some keywords for its own use and JavaScript also reserves some keywords that are not fully used in the current version of the language, But the most important reserved words in JavaScript (in alphabetical order) will be used in future JavaScript extensions:

abstract else instanceof super
boolean enum int switch
break export interface synchronized
byte extends let this
case false long throw
catch final native throws
char finally new transient
class float null true
const for package try
continue function private typeof
debugger goto protected var
default if public void
delete implements return volatile
do import short while
double in static with