Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

WEEK 11-MODULE 3

Comment in JavaScript
Comment is nothing but it is a statement which is not display on browser window. it is useful
to understand the which code is written for what purpose.
Comments are useful in every programming language to deliver message. It is used to add
information about the code, warnings or suggestions so that the end user or other developer
can easily interpret the code.
Types of JavaScript Comments
There are two types of comments are in JavaScript
 Single-line Comment
 Multi-line Comment

Example:

Javascript simple example to verify age of any person, if age is greater than 18 show
message adult otherwise show under 18.

JavaScript Example to verify age


Example JavaScript

<html>
<head>
<script>
function verify(){
var no;
no=Number(document.getElementById("age").value);
if(no<18)
{
alert("Under 18");
}
else
{
alert("You are Adult");
}
}
</script>
</head>
<body>
Enter your age:<input id="age"><br />
<button onclick="verify()">Click me</button>
</body>
</html>

JS Example:
<p id="demo">A Paragraph</p>
<button type="button" onclick="myFunction()">Try it</button>
<script>
function myFunction() {
    document.getElementById("demo").innerHTML = "Paragraph changed.";
}
</script>
JS Output:
document.getElementById("demo").innerHTML = 5 + 6;
document.write(5 + 6);
window.alert(5 + 6);
console.log(5 + 6);
<button onclick="document.write(5 + 6)">Try it</button>
JS Syntax:
JS code lines are separated by semicolon (;)
JS variables are defined using ‘var’ keyword
JS operators (+, -, *, /)
JS expression is a combination of values, variables, and operators, which computes to a value
JS comments (//, /* */)
JS and CamelCase (In JS variables are defined in CamelCase e.g firstName etc.)
JS Functions:
Block of code to perform some task
We have to call that function to perform that task
They avoid repetetion of code
We can use a function multiple times in our code
var x = myFunction(4, 3);
function myFunction(p1, p2) {
    return p1 * p2;      
}
JS Arrays:
A special variable which can hold multiple values at a time
var items = [“item1", “item2", “item3"];
var items = [
    “item1",
    “item2",
    “item3"
];
var items = new Array(“item1", “item2", “item3");

JS Objects:
Objects are also like variables but they contain multiple values
Objects contain properties + methods
Properties are pairs of ‘name’ and ‘value’
Methods are functions
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:”blue”, fullName: function()
{return this.firstName + “ ” + this.lastName;}};
person.firstName, person[‘firstName’]
person.fullName();
JS Conditional Statements:
Conditional statements are used to perform an action based on a condition
If statement, else, if else, switch statements
if (condition1) {
    block of code to be executed if condition1 is true
} else if (condition2) {
    block of code to be executed if the condition1 is false and condition2 is true
} else {
    block of code to be executed if the condition1 is false and condition2 is false
}
JS Conditional Statements:
Switch statement is used to perform different actions based on different cases
switch(expression) {
    case n:
        code block
        break;
    case n:
        code block
        break;
    default:
        code block
}
JS Loops (For, For/In):
Loops can execuste a block of code a number of times
for (i = 0; i < 5; i++) {
    text += "The number is " + i + "<br>";
}
For/In loop is used to iterate through the properties of an object
var person = {fname:"John", lname:"Doe", age:25}; 
var text = "";
var x;
for (x in person) {
    text += person[x];
}
JS Loops (While, do/while):
While loop, loops through a block of code as long as the condition is true
while (i < 10) {
    text += "The number is " + i;
    i++;
}
In do/while loop, block of code will be executed at least once even if the condition is false.
do {
    text += "The number is " + i;
    i++;
}
while (i < 10);
Visit this link for further knowledge: http://w3schools.com/js
ASSIGNMENT 8:
1. Create a variable called “YourFamilyName”, assign a value to it as asked in the
variablename.(value must be in all CAPS)
2. Create a variable called “Age”, assign a value with your age today.
3. Create a Javascript array that can hold values with your firstname, middlename and
lastname.

You might also like