04/07/2018

Types of JavaScript Statement Execution With Example

Statement Execution in JavaScript. Let’s now study JavaScript Statement execution. Statements are Instructions in a computer program and instructions executed by a computer. In this tutorial, we also talk about JavaScript Statement execution these instructions are called statements which are written in the programming language IE. C, C++, Java, Javascript, and any other programming language. A JavaScript program is a list of Instruction or programming statements, many Instruction or programming statements. 

Many exist in a single javascript program. We all know that for executing this javascript instruction we have different-different IDE(Integrated Development Environment) or editors or compilers but in javascript, these instructions or statements are executed by the web browsers ie. Google Chrome, Mozilla Firefox, Safari, Internet Explorer. 

Google Chrome is most recommended nowadays. Most JavaScript programs contain many JavaScript statements or instructions and these statements or instructions are executed, one by one, in the same order as they are written. Javascript statements(often called JavaScript code) or instructions are very simple cause they follow a similar rule as a programming language follows.

Types of JavaScript Statement Execution With Example

1. Javascript Statements Are Executed by The Web Browser


When Browser is trying to execute the JavaScript code, the browser understands that every statement is a command.


<!DOCTYPE html>
<html>

<body>
  <p>JavaScript statements<p>
      <p id="new"></p>
      <script>
        document.getElementById("new").innerHTML = " every statement is a command ";
      </script>
</body>

</html>

2. Javascript Statements Are Separated by Colons


Many statements are allowed in one line and statements are separated by a semicolon.


<!DOCTYPE html>
<html>

<body>
    <p>Many statements </p>
    <p id="MultistatementDemo"></p>
    <script>
        var i, j, k;
        i = 5; j = 9; k = i + j;
        document.getElementById("MultistatementDemo").innerHTML = k;
    </script>
</body>

</html>

3. Javascript Statements Are Executed by Line-by-line


Javascript statements are always executed in sequence, which means line-by-line rule followed by the browser when javascript statements run.


<!DOCTYPE html>
<html>

<body>
    <b>Sequence Execution of Statements</p>
        <p id="abc"></p>
        <script>
            var a, b, c;  // Statement 1
            a = 1;        // Statement 2
            b = 2;        // Statement 3
            c = a + b;    // Statement 4
            
            document.getElementById("abc").innerHTML ="Here c is " + c + ".";
        </script>
</body>

</html>

4) If a javascript code statement is going long, you can break it into multiple lines, It is treated as a single line in JavaScript, But to make the code readable the best practice is to break the line after a comma or after some operator.

Recommendation: Always try to break a long code in lines, it's easy to handle and it can save a lot of time, cause the mouse doesn't have any verticle scroll option.


<!DOCTYPE html>
<html>

<body>
    <p id="place"></p>
    <script>
        document.getElementById("place").innerHTML =
        "Hello JavaScript!";
        
        //This will also work
        //document.getElementById("place").
        //innerHTML =
        //"Hello JavaScript!";
    </script>
</body>

</html>

Last Words


If you have any doubts and need any help feel free to contact us or you can comment below, we are responsible to reply within 24 hours and you can like our Facebook page or Facebook group for any personal assistance. We are committed to helping students first so don't hesitate to ask any doubts or queries. Join us and become part of our organization.
Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: