JavaScript | Statements

05 Jul, 2022
The programming instructions written in a program in a programming language are known as statements.
Order of execution of Statements is the same as they are written.
Semicolons:Semicolons separate JavaScript statements.Semicolon marks the end of a statement in javascript.Example:<!DOCTYPE html><html>  <body>    <h2>Welcome</h2>      <p id="geek"></p>      <script>        // Statement 1        var a, b, c;          // Statement 2        a = 2;          // Statement 3        b = 3;          // Statement 4        c = a + b;          document.getElementById(          "geek").innerHTML =            "The value of c is " + c + ".";    </script>  </body>  </html>Output:Multiple statements on one line are allowed if they are separated with a semicolon.a=2;b=3;z=a+b;Code Blocks:JavaScript statements can be grouped together inside curly brackets. Such groups are known as code blocks. The purpose of grouping is to define statements to be executed together.Example:JavaScript function<!DOCTYPE html><html>  <body>    <p>Welcome</p>      <button type="button"             onclick="myFunction()">      Click Me!  </button>      <p id="geek1"></p>    <p id="geek2"></p>      <script>        function myFunction() {                        document.getElementById(              "geek1").innerHTML = "Hello";                        document.getElementById(              "geek2").innerHTML =               "How are you?";        }    </script>  </body>  </html>Output:Before Click:After Click:White Space:Javascript ignores multiple white spaces.Javascript would treat following 2 statements as same:Example:var a="Hello Geek";
var a = "Hello Geek";
Line Length and Line Breaks:Javascript code preferred line length by most programmers is upto 80 characters.The best place to break a code line in Javascript, if it doesn’t fits, is after an operator.Example:document.getElementById("geek1").innerHTML =
"Hello Geek!";
Keywords:Keywords are reserved words and cannot be used as variable name.A Javascript keyword tells about what kind of operation it will perform.Some commonly used keywords are:break: This is used to terminate a loop or switch.continue: This is used to skip a particular iteration in a loop and move to next iteration.do.... while: In this the statements written within do block are executed till the condition in while is true.for: It helps in executing a block of statements till the condition is true.function: This keyword is used to declare a function.return: This keyword is used to exit a function.switch: This helps in executing a block of codes depending on different cases.var: This keyword is used to declare a variable with a global scope.let: This keyword is used to declare a variable with block scope. Variables declared using let need not be initialized.const: This keyword is used to declare a variable with block scope. Variables declared using const must be initialized at the place of declaration.
Semicolons:Semicolons separate JavaScript statements.Semicolon marks the end of a statement in javascript.Example:<!DOCTYPE html><html>  <body>    <h2>Welcome</h2>      <p id="geek"></p>      <script>        // Statement 1        var a, b, c;          // Statement 2        a = 2;          // Statement 3        b = 3;          // Statement 4        c = a + b;          document.getElementById(          "geek").innerHTML =            "The value of c is " + c + ".";    </script>  </body>  </html>Output:Multiple statements on one line are allowed if they are separated with a semicolon.a=2;b=3;z=a+b;
Semicolons separate JavaScript statements.
Semicolon marks the end of a statement in javascript.Example:<!DOCTYPE html><html>  <body>    <h2>Welcome</h2>      <p id="geek"></p>      <script>        // Statement 1        var a, b, c;          // Statement 2        a = 2;          // Statement 3        b = 3;          // Statement 4        c = a + b;          document.getElementById(          "geek").innerHTML =            "The value of c is " + c + ".";    </script>  </body>  </html>Output:
Example:
<!DOCTYPE html><html>  <body>    <h2>Welcome</h2>      <p id="geek"></p>      <script>        // Statement 1        var a, b, c;          // Statement 2        a = 2;          // Statement 3        b = 3;          // Statement 4        c = a + b;          document.getElementById(          "geek").innerHTML =            "The value of c is " + c + ".";    </script>  </body>  </html>
Output:
Multiple statements on one line are allowed if they are separated with a semicolon.a=2;b=3;z=a+b;
a=2;b=3;z=a+b;
Code Blocks:JavaScript statements can be grouped together inside curly brackets. Such groups are known as code blocks. The purpose of grouping is to define statements to be executed together.Example:JavaScript function<!DOCTYPE html><html>  <body>    <p>Welcome</p>      <button type="button"             onclick="myFunction()">      Click Me!  </button>      <p id="geek1"></p>    <p id="geek2"></p>      <script>        function myFunction() {                        document.getElementById(              "geek1").innerHTML = "Hello";                        document.getElementById(              "geek2").innerHTML =               "How are you?";        }    </script>  </body>  </html>Output:Before Click:After Click:
Example:JavaScript function
<!DOCTYPE html><html>  <body>    <p>Welcome</p>      <button type="button"             onclick="myFunction()">      Click Me!  </button>      <p id="geek1"></p>    <p id="geek2"></p>      <script>        function myFunction() {                        document.getElementById(              "geek1").innerHTML = "Hello";                        document.getElementById(              "geek2").innerHTML =               "How are you?";        }    </script>  </body>  </html>
Output:Before Click:After Click:
White Space:Javascript ignores multiple white spaces.Javascript would treat following 2 statements as same:Example:var a="Hello Geek";
var a = "Hello Geek";

Javascript ignores multiple white spaces.
Javascript would treat following 2 statements as same:Example:
var a="Hello Geek";
var a = "Hello Geek";

Line Length and Line Breaks:Javascript code preferred line length by most programmers is upto 80 characters.The best place to break a code line in Javascript, if it doesn’t fits, is after an operator.Example:document.getElementById("geek1").innerHTML =
"Hello Geek!";

document.getElementById("geek1").innerHTML =
"Hello Geek!";

Keywords:Keywords are reserved words and cannot be used as variable name.A Javascript keyword tells about what kind of operation it will perform.Some commonly used keywords are:break: This is used to terminate a loop or switch.continue: This is used to skip a particular iteration in a loop and move to next iteration.do.... while: In this the statements written within do block are executed till the condition in while is true.for: It helps in executing a block of statements till the condition is true.function: This keyword is used to declare a function.return: This keyword is used to exit a function.switch: This helps in executing a block of codes depending on different cases.var: This keyword is used to declare a variable with a global scope.let: This keyword is used to declare a variable with block scope. Variables declared using let need not be initialized.const: This keyword is used to declare a variable with block scope. Variables declared using const must be initialized at the place of declaration.
Some commonly used keywords are:
break: This is used to terminate a loop or switch.
continue: This is used to skip a particular iteration in a loop and move to next iteration.
do.... while: In this the statements written within do block are executed till the condition in while is true.
for: It helps in executing a block of statements till the condition is true.
function: This keyword is used to declare a function.
return: This keyword is used to exit a function.
switch: This helps in executing a block of codes depending on different cases.
var: This keyword is used to declare a variable with a global scope.
let: This keyword is used to declare a variable with block scope. Variables declared using let need not be initialized.
const: This keyword is used to declare a variable with block scope. Variables declared using const must be initialized at the place of declaration.
farzams101
javascript-basics
Picked
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Hide or show elements in HTML using display property
Roadmap to Learn JavaScript For Beginners
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?