0% found this document useful (0 votes)
29 views

Java Script

Uploaded by

akshitbajpai6
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Java Script

Uploaded by

akshitbajpai6
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

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

-How We can add script with the html webpage

.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.

RULES FOR DECLEARING IDENTIFIER


 Identifier must start with alphabet character.
 White spaces are not allowed inside identifier naming declaration.
 Underscore is allowed as alphabet character.
 Uppercase & lower case are different because it is case sensitive language.
 The length of identifier become upto 64 character but 8 characters are significant or sufficient.
 Variables: variables are used to allocate the memory in computer for storing the
values that will be used in program
 Declaration Of variables:- we can declare variables using the below written syntax.

var varname1,varname2,varname3……etc;

Example:- var a,b,c;

 Variable initialization:- We can initialize variable using two ways-


 Compile Time Initialization
 Run time Initialization
 Compile time initialization:- In compile time initialization we will assign value on variables
at creating time of program.
Example-
<script>
var name, a;
name=”ram”;
a=1;
document.write(“name=”+name,”<br>”,”value of a”+a);
</script>
 Run time initialization:- In run time initialization we will assign the value on variable at the
execution time of program.

Example-

<script>

var name,a;

name=prompt(“enter name”);

a=parseInt(prompt(“enter the value of a”));

</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).

There are so many types of operators in java script-

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(--)

Increment & decrement operator are of two type-

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

Some commonly used comparison opertors are-

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.

Some commonly used logical operators are:-

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-

Variable_name= (condition)? (true_value) :( false_value);

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.

Example of compile time


i) <html>
<head>
<script>
var a;
a=”ram”
document.write(“name of student”+a);
</script>
</head>
<body>
</body>
</html>
ii) <html>
<head>
<script>
var a,b,c;
a=2;b=3;
c=a+b;
document.write(“addition”+c);
</script>
</head>
<body>
</body>
</html>
Example of run time initialization
i) <html>
<head>
<script>
var a;
a=prompt(“enter the name of student”);
document.write(“name of student”);
</script>
</head>
<body>
</body>
</html>
ii) <html>
<head>
<script>
var a,b,c;
a=parseInt(prompt(“enter the value of a=”));
b=parseInt(prompt(“enter the value of b=”));
c=a+b;
document.write(“addition”+c);
</script>
</head>
<body>
</body>
</html>
Q 1- WAP to print greatest number in given five number-

<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;

Q- WAP to print given no is even or odd(n%2==0)

Q WAP to print the given year is leap year or not (n%4==0)

c) NESTED IF STATEMENTS:- if inside another if statements are called nested if


statements.(it is used when conditions are dependent on each other)

Syntax:

if(condition)
{

if(condition)

Statements 1;

else

statements 2;

}}

else

Statements 3;

Q- WAP that prints greatest no in given 3 no using nested if-

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”);

}}

d) Ladder if statements:- An if statements can be followed by an optional else if


statements, which is very useful to test various condition using single if else statements
when using if, else if, else statements there are few points to keep in mind.(it is used
when condition are independent)

Syntax:-

if(condition 1)

Statements 1; (// executes when condition 1 is true)

else if(condition 2)

Statements 2; (// executes when condition 2 is true)

else if(condition 3)

Statements 3; (// executes when condition 3 is true)

}
------

-----

-----

else

Statements;(// executes when none of condition is true)

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)

Case 1: statement; break;

Case 2: statement; break;

Case 3: statement; break;

Case 4: statement; break;

--

---

--

Case n: statement n; break;

default: statement; break;

Note:- break statement is used for coming out from a particular case of switch or from
innermost loop.

Q- WAP TO print given character is vowel or consonant

Q- WAP to print month name by accepting month

You might also like