JAVASCRIPT DISPLAY POSSIBILITIES
JAVASCRIPT DISPLAY POSSIBILITIES
Variables,
Operators,
Control Flow Statements,
Popup Boxes
JavaScript Comment
The JavaScript comments are meaningful way to
deliver message. It is used to add information about
the code, warnings or suggestions so that end user
can easily interpret the code.
The JavaScript comment is ignored by the JavaScript
engine i.e. embedded in the browser.
Advantages of JavaScript comments
There are mainly two advantages of JavaScript
comments.
1. To make code easy to understand It can be used to
elaborate the code so that end user can easily
understand the code.
2. To avoid the unnecessary code It can also be used
to avoid the code being executed. Sometimes, we add
the code to perform some action. But after sometime,
there may be need to disable the code. In such case, it
is better to use comments.
Types of JavaScript Comments
There are two types of comments in JavaScript.
1. Single-line Comment
2. Multi-line Comment
OUTPUT:
Hello JavaScript
2. JavaScript Multi line Comment
It can be used to add single as well as multi line
comments. So, it is more convenient.
It is represented by forward slash with asterisk then
asterisk with forward slash.
Syntax:
/* your code here */
Example:
<html>
<body>
<script>
/* It is multi line comment.
It will not be displayed */
document.write(“Example of javascript
multiline comment");
</script>
</body>
</html>
OUTPUT:
Example of javascript multiline comment
JavaScript variable
A JavaScript variable is simply a name of storage
location.
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).
1. Name must start with a letter (a to z or A to Z),
underscore( _ ), or dollar( $ ) sign.
2. After first letter we can use digits (0 to 9), for example
value1.
3. JavaScript variables are case sensitive, for example x
and X are different variables.
Variables are used to store values (name = "John") or
expressions (sum = x + y).
Declare Variables in JavaScript
Before using a variable, you first need to declare it. You
have to use the keyword var to declare a variable like
this:
Syntax:
var var_name;
Example:
var name;
Assign a Value to the Variable
We can assign a value to the variable either while
declaring the variable or after declaring the variable.
Syntax:
var var_name = “value”;
Example:
var name = "John";
OR
var name;
name = "John";
1. JavaScript local variable
A JavaScript local variable is declared inside block or function.
It is accessible within the function or block only. For example:
<script>
function abc(){
var x=10;//local variable
}
</script>
OR
<script>
If(10<13){
var y=20;//JavaScript local variable
}
</script>
2. JavaScript global variable
A JavaScript global variable is accessible from any function. A variable
i.e. declared outside the function is known as global variable. For
example:
<html>
<body>
<script>
var data=200;//gloabal variable
function a(){
document.writeln(data);
}
a();
</script>
</body>
</html>
OUTPUT: