Java Script
Java Script
.Java script is an object oriented language scripting language that is used to add script with the html webpage
.There are two important things to note about this short piece of code
First, you should be aware that JavaScript is a "Weakly Typed" language. This means that when you define
your variables, you do not need to say what types they are: whether they are numbers, strings, objects, etc.
Second, note the semi-colon (;) at the end of the line. This tells your JavaScript interpreter that you are done
with what you are currently doing in the line. Although semi-colons are not necessary in JavaScript, it is a good
practice to get used to using them
.We can add script with the html webpage using html <script>……………………..</script> tag
.We can use script tag inside the head section and body section
TOKENS
Tokens are the basic building blocks of programs that is used to edit or design procedure code with the html
-webpage & tokens are divided in five category
i) Keywords: keywords are the predefined reserve words that are never modified or changed
have &have the fix meaning. Keywords are already exist in compiler & interpreter.
Some commonly used keywords are- html, head, title, body, do, while, for, switch, if, else, public,
private, break, default, …………..etc.
ii) Identifier:- Name assign to the program is called identifier like –variable name, class name,
function name, etc.
var varname1,varname2,varname3……etc;
Example-
<script>
var name,a;
name=prompt(“enter name”);
</script>
iii) String:-string is the collection of character-set that are always enclosed in ”. ”[double quotes]-
Character set are divided into three categories-
a) Alphabetic[a-z , A-Z]
b) Numeric[0-9]
c) Symbolic[*,+,-,/,@ etc.]
iv) Constant- The value of constant variable will never be changed.
v) Symbols- @,#,$,%,^,&,* etc.
OPERATOR
Operators are used to perform some operations on operand (variables).
1. Arithmetic Operator:-These operators can be applied on the number. Some commonly used
arithmetic operator are-
a. Addition(+)
b. Subtraction(-)
c. Multiplication(*)
d. Division(/)
e. Mod (%)[that is used to calculate remainder]
f. Increment(++)
g. Decrement(--)
i) Prefix
ii) Postfix
Example-
Let us consider we have 2 variables say x & y. Let assume-
Y=10;
Then
X=y++;//postfix
Now after the execution of above equation the value of x will be 10 & value of y will be 11, because
postfix expression first assign the value then increment/decrements the value of operand.
X=++y;//prefix
Now after the execution of above equation the value of x will be 11 & value of y will be 11, because
prefix expression first increment/decrements the value then assign it.
2. Comparison operators:- Comparison operators are used to compare value and/or variables.
The result of these type of operator is Boolean that is either true or false
a) Equal to(==)
b) Not equal to(!=)
c) Less than(<)
d) Greater than(>)
e) Less than equals(<=)
f) Greater than equals(>=)
3. Assignments operators:- Assignments operators are used to assign values to variables(i.e. =).
Some commonly used short hand assignment operators-
a)
+= (e.g. x+=y this expression is equivalent to x=x+y)
b)
-= (e.g. x-=y this expression is equivalent to x=x-y)
c)
*= (e.g. x*=y this expression is equivalent to x=x*y)
d)
/= (e.g. x/=y this expression is equivalent to x=x/y)
e)
%= (e.g. x%=y this expression is equivalent to x=x%y)
4. Logical Operators:- Logical Operators are used when we start to combine more than one
conditional statements in a single statements.
a) Logical and(&&)
b) Logical or(||)
c) Logical not(!)
5. Conditional operators:- It is used when we want to assign a value to a variable based on some
condition.
Syntax-
e.g.- a=(5>10)?5:10;
note- parseInt()- It is inbuilt java script method/function that convert textual/string value into a integer.
prompt- To take input from user.
<html>
<head>
<script>
Var a,b,c,d,n1,n2,n3,n4;
n1=parseInt(prompt(“enter the value of n1=”));
n2=parseInt(prompt(“enter the value of n2=”));
n3=parseInt(prompt(“enter the value of n3=”));
n4=parseInt(prompt(“enter the value of n4=”));
n5=parseInt(prompt(“enter the value of n5=”));
a=(n1>n2)?n1:n2;
b=(a>n3)?a:n3;
c=(b>n4)?b:n4;
d=(c>n5)?c:n5;
document.write(“addition”+d);
</script>
</head>
CONDITIONAL STATEMENTS
Conditional statements are used to solve Boolean expression and these statements are divided
into five category-
a) Simple if statement
b) If----- else statements
c) Nested if statements
d) Ladder if statements
e) Switch case statements
a) Simple If Statements:- In simple if statement the condition will be checked if condition
become true then block of if statement will be executed otherwise control will transfer
outer of the block.
Syntax:
if(condition)
Statement;
b) If-else statements:-it is also used to check conditions if condition is true then block of “
if statement” will be executed otherwise “ else” block will be executed but atleast one
block of statement must be executed.
Syntax:-
if(condition)
Statement1;
else
Statement 2;
Syntax:
if(condition)
{
if(condition)
Statements 1;
else
statements 2;
}}
else
Statements 3;
if(a>b)
if(a>c)
document.write(“a is greater”);
else
document.write(“c is greater”);
}}
else
{
if(b>c)
document.write(“b is greater”);
else
document.write(“c is greater”);
}}
Syntax:-
if(condition 1)
else if(condition 2)
else if(condition 3)
}
------
-----
-----
else
e) Switch case statements:- Switch statement is basically used for multiway branching. It
is used when we want to select one case from multiple choices.
Syntax:-
switch(variable_name)
--
---
--
Note:- break statement is used for coming out from a particular case of switch or from
innermost loop.