Showing posts with label Statements in JavaScript. Show all posts
Showing posts with label Statements in JavaScript. Show all posts

JavaScript: Statements

 Statements in Javascript are the step by step instructions to be executed by the computer. A JavaScript program is built up of its programming statements.

Javascript statements may composed of :

Variable

Value

Operators

Expressions

Keywords and

Comments

The statements are inserted in HTML document between <script> and </script> tags and run by web browsers.

see an easy example:

<html>

<body>

<h2>JavaScript Statements</h2>

<p>JavaScript Statements are the step by step instructions to be executed by computer. </p>

<p id="demo"></p>

<script>

document.getElementById("demo").innerHTML = "Hello World.";

</script>

</body>

</html>

Now try the above program yoursef  in 'try it editor' given here.
Try it yourself editor
 
Code
Output

 A JavaScript program may consists of many statements and these statements are executed one by one in  the order, in which they are written. Each statement separates from each other by ;(semicolon).

For ex:

<html>

<body>

<h2>JavaScript Statements</h2>

<p>JavaScript statements are instructions executed one by one.</p>

<p id="demo"></p>

<script>

var a, b, c;    //declair three variables named a, b and c

a = 5;           //assign the value 5 to a

b = 6;           //assign the value 6 to b

c = a + b;     //assign the value of a+b to c

document.getElementById("demo").innerHTML = c;

</script>

</body>

</html>

Now try it yourself in below editor

Try it yourself editor
 
Code
Output

 In above editor change any value of variable a or b, and see the result. We can use 'Let' keyword to store a direct value into a variable(like c = 7 + 6). But when we store a variable into another variable (like c =a + b) then we use 'var' keyword.

We can also write multiple statements in one line separated from each other by semicolon. 

like     var a, b, c;         a = 5;           b = 6;           

            c = a + b;

You can try in above editor window.

JavaScript ignores the multiple spaces inside statement. For example c=a+b and c = a + b, both are equivalent.
For best readability in JavaScript, just ignore a line is too much longer. You can break it after a comma or an operator, like:

document.getElementById("demo").innerHTML = "Hello World.";

Instead it you can write

document.getElementById("demo").innerHTML = 
"Hello World.";

JavaScript's Code Blocks
We can execute multiple statements together to show a bulk information. This is done by writing the statements in a code block inside  curly brackets.
For example.
<html>
<body>
<h2>JavaScript's Code Block</h2>
<p>To show the bulk information, it is used</p>
<button type="button" onclick="myFunction()">Click Me!</button>
<p id="test1"></p>
<p id="test2"></p>
<script>
function myFunction() {
  document.getElementById("test1").innerHTML = "Hello World!";
  document.getElementById("test2").innerHTML = "I am your new friend.";
}
</script>
</body>
</html>
Try it in editor:

Try it yourself editor
 
Code
Output

 JavaScript Keywords

JavaScript statement often starts with a keyword to identify the action to be performed. These keywords are reserved words for JavaScript. Let's know some most usual keywords of JavaScript:

Keyword

Description

let

Assign a value inside a variable

var

Assign a variable inside a variable

const

Assign  constant number or string(value cant be changed during execution)

if

Identify a block of statements to be executed on given condition

switch

Identify a block of statements to be executed in different cases

for

Identify a block of statements to be executed in a loop

function

Declares a function

return

Exits a function

try

Implements error handling to a block of statements





















Python: Functions