Professional Documents
Culture Documents
JAVASCRIPTS
JAVASCRIPT ORIGIN
Variable Names
There are rules and conventions in naming variables in any programming
language. It is good practice to use descriptive names for variables. The
following are the JavaScript rules:
• The variable name must start with a letter or an underscore.
firstName or _myName
• You can use numbers in a variable name, but not as the first
character.
name01 or tuition$
document.write(x + "<br>");
document.write(y +
"<br>");
document.write(z + "<br>");
</script> document.write is used to write the output
directly into the document output.
</body>
</html>
DATA TYPES
• Undefined - var x;
• String - var x=“college”;
• Number - var x=8; var x=9.30;
• Boolean, - var x=true;
• Array, - var
cars=["Saab","Volvo","BMW"];
• Null - var s=null;
OPERATORS
What is an operator?
Simple answer can be given using expression 4 + 5 is
equal to 9. Here 4 and 5 are called operands and + is
called operator. JavaScript language supports
following type of operators.
• Arithmetic Operators
• Comparision Operators
• Logical (or Relational) Operators
• Assignment Operators
• Conditional (or ternary) Operators
THE ARITHMETIC OPERATORS:
Operator Description Example
+ Adds two operands A + B will give
30
- Subtracts second operand from the first A - B will give -
10
* Multiply both operands A * B will give
200
/ Divide numerator by denumerator B / A will give 2
% Modulus Operator and remainder of B % A will give 0
after an integer division
++ Increment operator, increases integer A++ will give 11
value by one
-- Decrement operator, decreases integer A-- will give 9
value by one
OTHER OPERATORS
= x=y x=5
If Statement
• Use the if statement to execute some code only if a
specified condition is true.
• if (condition)
{
code to be executed if condition is true
}
• Example:
if (time<10)
{
x="Good day";
}
CONT..
If...else Statement
Use the if....else statement to execute some code if a condition is true and
another code if the condition is not true.
• if (condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is not true
}
• Example:
If(a!=b)
{document.write(“both are unequal”);
}
else
{document.write(“equal”);
}
CONT..
Syntax
if (condition1)
{
code to be executed if condition1 is true
}
else if (condition2)
{
code to be executed if condition2 is true
}
else
{
code to be executed if neither condition1 nor condition2 is true
}
FOR LOOP
• Loops are handy, if you want to run the same code over
and over again, each time with a different value.
• The for loop has the following syntax:
• for (statement 1; statement 2; statement 3)
{
the code block to be executed
}
• Statement 1 is executed before the loop (the code
block) starts.
• Statement 2 defines the condition for running the loop
(the code block).
• Statement 3 is executed each time after the loop (the
code block) has been executed.
EXAMPLE- FOR LOOP
• The do/while loop is a variant of the while loop. This loop will
execute the code block once, before checking if the condition is
true, then it will repeat the loop as long as the condition is true.
Syntax
do
{
code block to be executed
}
while (condition);
Example:
var i=0;
do
{
document.write("The number is " + i + "<br>");
i++;
}
while (i<5);
ACTIVITIES TO DO
1. Wirte a javascript program to mutiply two numbers.
2. Wite a javascript program to display the following text: welcome to
javascript
3. Write a javascript program for the following.
assign a=10 and b=20. Check if a is equal to b.
4. Write a javascript program to find the biggest of three numbers.
5. Wirte a javascript program to display the following result using for loop:
Count:0
Count:1
Count:2
Count:3
Count:4
Count:5
6. Write a javascript code to display the following output using while loop.
X=3
X=2
X=1