javascript ক্যোয়ারীর জন্য প্রসঙ্গ অনুসারে বাছাই করা পোস্ট দেখানো হচ্ছে৷ তারিখ অনুসারে বাছাই করুন সব পোস্ট দেখান
javascript ক্যোয়ারীর জন্য প্রসঙ্গ অনুসারে বাছাই করা পোস্ট দেখানো হচ্ছে৷ তারিখ অনুসারে বাছাই করুন সব পোস্ট দেখান

JavaScript Statements, Semicolons, White Space and Comments

JavaScript statements are composed of: 
  • Values, 
  • Operators, 
  • Expressions, 
  • Keywords and
  • Comments.
The following statement tells the browser to write "Hello JavaScript." inside an HTML element with id="example":
Example:
<!DOCTYPE html> 
<html> 
<body> 
<h2>JavaScript Statement Example</h2> 
<p>In this Example, JavaScript statements are executed by the browser.</p> 
<p id="demo"></p> 
<script> 
document.getElementById("example").innerHTML = "Hello JavaScript."; 
</script> 
</body> 
</html>
Most JavaScript programs contain many JavaScript statements. 
Note: JavaScript programs (and JavaScript statements) are often called JavaScript code.

Semicolons ( ; ):
Semicolons separate JavaScript statements. You should add a semicolon at the end of each executable statement.
Example:
<!DOCTYPE html> 
<html> 
<body> 
<h2>JavaScript Semicolons</h2> 
<p>JavaScript Semicolons separates JavaScript Statemant.</p> 
<p id="example"></p> 
<script> 
var x, y, z; 
x = 10; 
y = 12; 
z = x + y; 
document.getElementById("example").innerHTML = z; 
</script> 
</body> 
</html>
Multiple statements on one line are allowed, When separated by semicolons:
Example:
<!DOCTYPE html> 
<html> 
<body> 
<h2>JavaScript Semicolons</h2> 
<p>JavaScript Semicolons separates JavaScript Statemant.</p> 
<p id="example"></p> 
<script> 
var x, y, z; 
x = 11; y = 13; z = x + y; 
document.getElementById("example").innerHTML = z; 
</script> 
</body> 
</html>
JavaScript White Space:
JavaScript ignores multiple spaces. To make it more readable just add white space to your script. The following lines are equivalent:
var person = "amit"; 
var person="amit";
Put spaces around operators ( = + - * / ) is a good practice.
Example:
var a = b + c;
var person = "amit";
JavaScript Comments:
To make Code more readable and explain JavaScript code use javascript comments.

Two Types of Comments in JavaScript:
  1. Single Line Comments
  2. Multi-line Comments
Single Line Comments:
Single line comments start with //. 
Javascript can not execute the comments line
Example of single line comment:
<!DOCTYPE html> 
<html> 
<body> 
<h1 id="id1"></h1> 
<p id="id2"></p> 
<script> 
// Change heading 
document.getElementById("id1").innerHTML = "JavaScript Single Line Comments"; 
// Change paragraph 
document.getElementById("id2").innerHTML = "A Simple paragraph."; 
</script> 
</body> 
</html>
Example of single line comment:
<!DOCTYPE html> 
<html> 
<body> 
<h2>JavaScript Single Line Comments</h2> 
<p id="example"></p> 
<script> 
var a = 10;    // Declare a, give it the value of 10 
var b = a + 2;  // Declare b, give it the value of a + 2
document.getElementById("example").innerHTML = b; // Write b to example
</script> 
</body> 
</html>
Multi-line Comments:
It start with /* and end with */. 
Ignored by JavaScript if any text between /* and */ will be. 
Example of Multi line comment:
<!DOCTYPE html> 
<html> 
<body> 
<h1 id="heading1"></h1> 
<p id="paragraph1"></p> 
<script> 
/* The code below will change 
the heading with id = "heading1
and the paragraph with id = "paragraph1" */ 
document.getElementById("heading1").innerHTML = "JavaScript Multi Line Comments"; 
document.getElementById("paragraph1").innerHTML = "A Simple paragraph."; </script> 
</body> 
</html>
Note: Multi Line Comments are often used for formal documentation.
Project Source Code:
<!DOCTYPE html> 
<html> 
<body> 
<h2>JavaScript Statements, Semicolons, White Space and Comments</h2> 
<p id="example"></p> 
</body> 
</html> 
<script type="text/javascript"> 
var x, y, z; // that is a statement 
x = 5; // that is also statement 
y = 6; // that is also statement 
z = x + y; //  ; samicolon 
// - single line comment 
/*-multi line comment*/ 
// ; semicolon 

alert(z);
</script>
Read More

Where to use JavaScript Code in the HTML Pages (Internal JavaScript)

Ways to include JavaScript Code in the HTML web Pages:
  1. Internal JavaScript
  2. External JavaScript
In this Article, I'm going to discus about Internal JavaScript. 

JavaScript Where to uses?
Internal Javascript code can be placed anywhere in the web page inside  <script> tags. Many programmers who choose to place their JavaScript code before the tag.

In HTML DOM, JavaScript Code must be inserted between Open Tag <script> and the End Tag </script>

Example :

<script> 
 document.getElementById("demo").innerHTML = "My First JavaScript"; 
</script>
Any number of scripts can be located in an HTML document. JavaScript Code can be used in <head> or <body> section of an HTML page, or in both section.

How to use JavaScript Code in <head> Section?
In the following example, JavaScript code is allocated in the <head> section of  HTML web page.
This example show The function will be called when the button is clicked.
<!DOCTYPE html> 
<html> 
<head> 
<style> 
body{ 
background-color: gray; 
</style> 
<script> 
function newFunction() { 
document.getElementById("appliedID").innerHTML = "New applied paragraph after button clicked."; 
</script> 
</head> 
<body> 
<h2>JavaScript code in Head section</h2> 
<p id="appliedID">Predefined Paragraph.</p> 
<button type="button" onclick="newFunction()">Click</button> 
</body> 
</html>

Result: When you will run the above code, it will show the following interface. Here if you click the "Click" button then the paragraph "Predefined Paragraph." will be changed to "New applied paragraph after button clicked."

Result: After clicking the button the following interface will be appeared.
How to use JavaScript Code in <body> Section?
In the following example, JavaScript code is allocated in the <body> section of  HTML web page.
This example show The function will be called when the button is clicked.
<!DOCTYPE html> 
<html> 
<style> 
body{ 
background-color: gray; 
</style> 
<body> 
<h2>JavaScript code in Body section</h2> 
<p id="appliedID">Predefined Paragraph.</p> 
<button type="button" onclick="newFunction()">Click</button> 
<script> 
function newFunction() { 
document.getElementById("appliedID").innerHTML = "New applied paragraph after button clicked."; 
</script> 
</body> 
</html>

Result: When you will run the above code, it will show the following interface. Here if you click the "Click" button then the paragraph "Predefined Paragraph." will be changed to "New applied paragraph after button clicked."

Result: After clicking the button the following interface will be appeared.

In the following video i'm showing all the steps about how to include External JavaScript Code in the HTML web pages.

Source Code of the above project:
<!DOCTYPE html> 
<html> 
<body> 
<h2>The way to do Internal JavaScript Code?</h2> 
<p id="demo">Internal JavaScript.</p> 
<button type="button" onclick="document.getElementById('demo').style.color = 'red'; ">Click Me!</button> 
</body> 
</html>
Read More

JavaScript Variables

Variables are used to store data values. Use "var" keyword befor the variable name Example: var x;  to declare javascript variables. (=) is called equal sign that is used to assign values to variables.

Example:

var a = 10; // a is variable name
Variables are containers for storing data values.

There are two types of variables in JavaScript :

  1. local variable and 
  2. global variable.
There are some rules while declaring a JavaScript variable (also known as identifiers):
  • Variable name must start with a letter like (a to z or A to Z), underscore( _ ), or dollar( $ ) sign. 
  • After first letter we can use digits (0 to 9), Example variable2. 
  • JS variables are case sensitive, Example a and A are different variables.
Correct JavaScript variables:
var a = 100; 
var _variable="Bangla";
Incorrect JavaScript variables:
var  521=10; 
var *ba=450;
Let’s see a simple example of JavaScript variable:
<html> 
<body> 
<h2>JavaScript Variables Example</h2> 
</body> 
</html>
<script> 
var a = 50; 
var b = 5; 
var c = a + b; 
document.write(c); 
</script>

Result:
JavaScript Variables Example
55
JavaScript local variable:
Local variable is declared inside block or function in javascrupt. Local variable is accessible within the function or block only. 

Example:

<script>
 function xyz(){
  var a = 100; // JavaScript local variable
 }
</script>

or

<script> 
 If(a<b){ 
  var a = 15; // JavaScript local variable 
 } 
</script>
JavaScript global variable:
A JavaScript global variable is accessible from any function. A variable i.e. declared outside the function or declared with window object is known as global variable.
Example:
<html> 
<body> 
<script> 
var global_variable= 100; //global variable 
function abc(){ 
document.writeln(global_variable); 
function xyz(){ 
document.writeln(global_variable); 
abc(); //call JavaScript function 
xyz(); 
</script> 
</body> 
</html>
Project Source Code:
<!DOCTYPE html> 
<html> 
<body> 
<h2>JavaScript Variables</h2> 
<h4>variables are used to store data values.</h4> <h4>JavaScript always use the <b style="color: red; font-weight: bold">var</b> example: var x; keyword to declare javascript variables.</h4> 
<h4>An <b style="color: red; font-weight: bold">equal sign (=)</b> is used to assign values to variables. </h4> 
<p>Here x is a variable. Then assigned the value to variable x</p> 
<p id="example"></p> 
</body> 
</html> 
<script type="text/javascript"> 
var x = 10 + 12; // here = is used to assign the value of variable x and used var keyword to declare the variable document.getElementById("example").innerHTML = "The value of Variable x is ="+x; 
</script>
Read More