JavaScript Statement and Syntax

Published by StudyMuch on

JavaScript StudyMuch.in

JavaScript Statement and Syntax

In this tutorial you will learn about JavaScript Statement and Syntax, here you learn how to write JavaScript Statement like single-line statement, two single-line statement and grouping JavaScript statement and also read about JavaScript Syntax. Here you learn all the statement with programming examples, so that you can understand better.

JavaScript Statements

The programming instructions are written in a programming language are known as statements.

JavaScript statements are composed of value such as operators, Keywords, Expression and Comments statements are “instructions” to be executed by the web browser.

JavaScript programs consist of statements with appropriate syntax. A single JavaScript statement may span a single or multiple lines.

JavaScript statements should be ended or be separated by semicolons ( ; ).

Below the example of all the statements; single-line, two single-line and grouping JavaScript statements.

Programming Examples of single-line statement;

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible"
content="IE=edge">
    <meta name="viewport"
content="width=device-width, 
initial-scale=1.0">
 <title>JavaScript Statement</title>
</head>
<body>
<p id="studymuch">Single Line Element</p>
  <script>
   document.getElementById("studymuch")
  .innerHTML = "Hello Programmer!";
  </script>
</body>
</html>

You can see above programming this is single-line statement, its output is “Hello Programmer”; Programming Example of two single-line statements;

Below is a program that consists of two single-line statement.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible"
content="IE=edge">
    <meta name="viewport" 
content="width=device-width,
 initial-scale=1.0">
    <title>JavaScript Statement</title>
</head>
<body>
<p id="first">Single Line Element</p>
<p id="second">two single-line Element</p>
    <script>
    document.getElementById("first")
   .innerHTML = "Hellow Programmer!";
   document.getElementById("second")
  .innerHTML ="You are welcome in 
   StudyMuch.";
    </script>
</body>
</html>

You can see above programming example, that is the two single-line statements. You can have many statements as you need when writing programs.

The output of above programming example is given below;

JavaScript Statement and Syntax


Grouping JavaScript Statements

JavaScript statements can be grouped inside curly brackets {}, these are called “Code Blocks“.

Code blocks are used to make statements execute together; they are commonly used in functions of JavaScript.

Programming examples of Grouping Statement;

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible"
content="IE=edge">
    <meta name="viewport"
content="width=device-width, 
initial-scale=1.0">
    <title>JavaScript Statements</title>
</head>
<body>
 <p id="change">I am a Group statement.</p>
 <button type="button"onclick="myfunc()">
   Change Content</button>
 <script>
   function myfunc(){
    document.getElementById("change").
   innerHTML = "Hello Programmer!";
        }
 </script>
</body>
</html>

Above you can see the programming example of grouping statement, its output is given below;

JavaScript Statement and Syntax

When you click the “Change Content” Button the content will be change, change content output is given below;

JavaScript Statement and Syntax

When you click the Change Content Button this Content will be printed, that you can see in above image. Now you will learn the JavaScript Syntax below;

JavaScript Syntax

The syntax of JavaScript is the set of rules that define a correctly structured JavaScript programs. JavaScript follows a set rule that called Syntax.

Variable Declaration

In math, we use variable to hold value, it works the same way in JavaScript, a value can be assigned to a value. This is called the declaring variables.

To declare a variable, use the var keyword to create it, and then an equal sign
= to assign value.

Look the Programming Example;

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible"
content="IE=edge">
    <meta name="viewport"
content="width=device-width, 
initial-scale=1.0">
    <title>JavaScript Statements</title>
</head>
<body>
    <Script>
        var num = 7007  ;
        document.write(num);
    </Script>
</body>
</html>

You can see the programming example above; this program was written line Java Program, this program produce output is given below in picture.

JavaScript Statement and Syntax

The num is the example in above programming is called “identifier“. JavaScript identifier are used to name variable.

Look one more example;

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible"
content="IE=edge">
    <meta name="viewport"
content="width=device-width, 
initial-scale=1.0">
    <title>JavaScript Syntax</title>
</head>
<body>
 <Script>
    var num = 7;
    var number = 7;
    var sum = number + num;
    document.write(sum);
 </Script>
</body>
</html>

In above programming example, the both variable is different, one is num and another is number, we sum in this programming both variable. The output of this programming example sum of num and number variable is 14.

So, in this you learned about JavaScript Syntax and Statements with programming examples, I hope you read this tutorial well and learn new something.

Read Also –


5 Comments

graliontorile · August 19, 2022 at 12:33 am

I conceive this site contains some real wonderful information for everyone :D. “Believe those who are seeking the truth doubt those who find it.” by Andre Gide.

zoritoler imol · September 14, 2022 at 10:43 pm

After study a few of the blog posts on your website now, and I truly like your way of blogging. I bookmarked it to my bookmark website list and will be checking back soon. Pls check out my web site as well and let me know what you think.

zoritoler imol · November 2, 2022 at 7:38 am

Really clean site, thankyou for this post.

zoritoler imol · November 18, 2022 at 9:58 pm

Hello my friend! I want to say that this article is awesome, great written and come with almost all significant infos. I?¦d like to peer extra posts like this .

best seo services · August 10, 2023 at 12:23 pm

Awesome blog article.Really thank you! Keep writing.

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *