JavaScript Statements

Statements in JavaScript are line-by-line codes that are being created to develop a small-scale or a large-scale JavaScript application. For example:

HTML with JavaScript Code
let x;               // statement 1
x = 10;              // statement 2
document.write(x);   // statement 3
Output
10

Note - Anything written after // considered as a comment, and the text will not be executed.

In above example, the first statement declares a variable, the second statement initializes a value 10 to the variable, and finally using the third statement, the value of x was printed on the document using the document.write() method.

A statement can be called as an instruction of a computer program.

A computer software is a collection of instructions.

Note - In JavaScript, it is not required that a statement expands only in a single line. One statement can be written in multiple lines. Also a semi-colon (;) is not necessary to end a statement. Therefore, above example can also be written as:

<script>
   let x
   x = 10
   document.write(x)
</script>

Or

<script>
   let x
   x 
   = 10
   document
   .write(x)
</script>

But writing a simple single statement in a single line is recommended to avoid any misunderstanding about the code/statement.

JavaScript Online Test


« Previous Tutorial Next Tutorial »