JS Statements : in JavaScript

Statement in JavaScript :

  • Firstly check whether JS is enabled in Browser or not (Mozilla | IE | Opera)
  • Executable single line of Script is called as Statement in JavaScript.
  • JavaScript Code contain sequence of statements that are executed by browser.
  • Every Executable JavaScript Statements is terminated by semicolon (optional)
  • Statement is a command to a browser.

Type of JavaScript Statements -

  1. Conditional Statement [i.e If-else Statement]
if(condition)
 {
 ------
 ------
 }
  1. Declaration Statement
var ivar;
  1. Assignment Statement
ivar = 100;
  1. Executable Statement
document.write("Hello JavaScript");

Some Rules of JavaScript Statements

[checklist]
  • JavaScript Statement is case Sensitive
  • JavaScript Statement ends with semicolon (optional)
  • JavaScript Statement may contain HTML Tags.
  • JavaScript Statements can be embedded inside block
[/checklist] consider one by one scenario -


1. JavaScript is case Sensitive

Error No 1 :

document.Write("Hello JS");

this statement will throw error.instead it should be like this -

document.write("Hello JS");

Error No 2 :

var message;
document.write(Message);

message and Message will be considered as two different variables.


2. JavaScript Statement ends with semicolon (optional)

  1. According to the JavaScript standard ,semicolon is optional .
  2. The browser is supposed to interpret the end of the line as the end of the statement.
  3. Because of this you will often see examples without the semicolon at the end.

3. JavaScript Statements can be embedded inside block

  1. Pair of Curly braces is used to embed JavaScript Statements.
  2. Once embedded in Pair , Group of JavaScript Statements is called as “Group“.
<script type="text/javascript">
{
document.write("<h1>This is a heading</h1>");
document.write("<p>This is a paragraph.</p>");
document.write("<p>This is another paragraph.</p>");
}
</script>

JavaScript Statements may contain HTML Tags as shown in Program.