JavaScript is an object-based, client side scripting language. It is a incorporation of HTML and programming language.
JavaScript can also be used for server side scripting. Client side scripting means which run on client machine and server side scripting means that runs on a web server. JavaScript is not very much powerful for server application.
Comparison between JavaScript and Java
JavaScript Java
JavaScript is a script language. Java is a programming language.
It is an interpreted language. It is a compiler as well as interpreted Language.
It is not required to declare data type of It is compulsory to declare data types of variables
the variables of the JavaScript in Java
JavaScript is an Object-based Language Java is an object-oriented language.
There are also some similarities between JavaScript and java like:
JavaScript and Java uses the same control flow statements like if ….else, switch…. case construct; loop like while loop, do …while loop and for loop. Both languages follow the same syntax and rules to declare names of variables, functions etc. to write programs and both are case sensitive languages.
Objects
All things that exists in real world ranging from living to non-living. We can touch and feel an object, like a human being or a chair which are objects.
Method:
Methods are the properties of the objects. JavaScript can manipulate behavior and actions of many objects. Document object’s write() method writes the text given within the parenthesis (” “) on the document area.
Events
Browser executes web page line by line. We can change the way in which the browser responds. For this purpose we use Events. Thus events can be defined as those sets of links/codes by which sequence of execution within a scripted program can controlled/altered. For instance, when a user clicks a button, an event is generated which informs the browser about occurrence of an action.
Variable
Variables can be defined as places/ spaces in the memory storing data. Variables have names, which can use characters, digits and underscore. There are some rules for naming variable.
Spaces are not allowed.
Punction character is not allowed.
Variable name should start with a letter or underscore. Length can be of your choice
Variable name are case sensitive(small letter or capital both are same).
The way to declare variable is
Keyword var can be used, which is optional
Assign value to the variable which you want.
Syntax
var x=1;
DATA TYPES
Data type refers what types of data can be stored in a particular variable. There are different type of data types in JavaScript. In javascript we define data type before the variable.
Number: It includes both integers and floating – point number
String : Which includes one or more character of text
Boolean or Logical : These have two values true or false
Null : Null value is a value which is undefined.
Program of JavaScript
<Script Language= “JavaScript”>
document.write(“Hello World”);
document.write(“<br>” + “=========”);
</Script>
Note: Script tag can be write with in <HTML> and </Html> tag.
Example,
<html>
<head>
<Script Language= “JavaScript”>
document.write(“New Century Computer Point”);
document.write(“<br>”+ “====================”);
</Script>
</Head>
</Html>
Write a program in JavaScript to print the sum of two numbers.
Write a program to calculate simple Interest. Here (PV=5000, Rate=12%, Time=1)
Operators
Operators are the special symbols which performs the calculating , comparison, etc. there are different types of operators in JavaScript.
1. Arithmetic Operators :
+ Addition
– Subtraction
* Multiply
/ Divide
% Modulation (Reminder Value)
These operators can be used for mathematical calculations. Generally there are two elements one is operator and other is operand.
variables or constants are the operand and symbols are the operators.
Example,
X + Y = Z
X, Y and Z are the operand .
+ and = are the operators.
2. Relational Operators.
Relational operators mainly occur to compare two or more then two values. There are two types of relational operators.
i. Comparison Operator
> Greater than
< Less than
>= Greater than or equal to
< = Less than or Equal to
ii. Equality Operator.
= = Equality
! = Not Equality
Logical Operators
To show the true or false. There are mainly three types of logical operators in JavaScript.
i. && Logical AND
Eg.
if(eng>=20 && mat >=20)
{
document.write( “Pass”);
}
else
{
document.write(“Fail”);
}
i. | | Logical OR
if(ans= = ‘y’ | | ans= = ‘Y’)
Alert(“Good luck”);
iii. ! logical Not
Example,
while (x ! 0)
{
document.write(“a”);
}
Unary operators
JavaScript supports very important two operators increment and decrement. These operators are call unary operators.
+ + Increment ( increment by 1)
– – Decrement (Decrement by 1)
example,
x + + ; prefix x= x+1;
+ + x ; Post fix x=1+x;
x – – ; x=x-1
Control Statement
JavaScript is a object based scripting Language. Doing programming, we need to control or execute the required statement according to condition. There are different control statements in JavaScript.
1. Decision Making :
Decision making can be divide in two categories.
i if else statement
if the given condition is true then the true statement will execute(the statement before else), if the condition is false then the false statement or statement after else will execute.
Syntax 1:
if(conditional expression)
{
statement;
statement;
}
else
{
statement;
statement;
}
1. Write a program to print the greater number of the given two numbers.
Syntax – 2
if(condition)
{
statement;
statement;
}
else if(condition)
{
statement;
statement;
}
else
{
statement;
statement;
}
Syntax: -3
if (conditional Expression)
{
if (Conditional _Expression)
{
statement1;
}
else
{
statement;
}
}
else if(conditional expression)
{
statement;
}
else
{
statement;
}
Example,
int x, y, z;
x=10;
y=22;
z=15;
if(x>y)
{
if(x>z)
{
document.write(“X is greatest”);
}
else
{
document.write(“Z is greatest”);
}
}
else if(y>z)
{
document.write(“y is greatest”);
}
else
{
document.write(“Z is greatest”);
}
ii. Switch Case statement
The switch case statement is also used for decision making. It always be used for multiple selection of the options to run the particular statement. The use of this statement is that to make menu.
Syntax:
switch (Expression)
{
case expression 1:
statement;
break;
case expression 2:
statement ;
break;
case expression 3:
statement;
break;
case expression 4:
statement;
break;
default:
statement;
break;
}
Note : Switch case statement takes two keywords, break and default.
Break : to wait the program on the screen.
default : Otherwise to run false statement.
Example,
var day=1;
switch(day)
{
case 1:
document.write(“Sunday”);
break;
case 2:
document.write(“Monday”);
break;
……………
…………
default:
document.write(“Invild day”);
break;
}
}
2. Looping
i. For loop
It can be used to continue execute the program till the given the condition is true. It takes the three statements. The first statement for initial condition, second is stop condition and the third is increment or decrement.
Syntax:
for(initial condition; last or stop condition; increment or decrement)
{
statement;
}
e.g.
for(x=1;x<=10;x++)
{
document.write(“<br>”+x);
}
ii. While loop
It is also loop for the continue the program.
Syntax:
while(condition)
{
statement;
statement;
}
E.g.
var x=1;
while(x<=10)
{
document.write(“<br>”+x);
x=x+1;
}
iii. do while loop
It can be used to continue execute the program till the given condition will meet. It is also loop statement as while.
syntax:
do
{
statement1;
statement2;
statement …n;
}
while(conditional expression);
example,
int x=1;
do
{
document.write(“<BR>”+x);
x=x+1;
}
while(x<=10);
Write a program whether the given number odd or even.
Write a program to print multiplication table of any number by using for loop, while loop, and do .. while loop.
Write a program to print the fifonacci series ( 1,2,3,5,8,13,21…..) by using while and do while loop.
Write a program to print the square number and cube number of 1 to 10.
Write a program to print the number in descending order (10 to 1) by using for loop, while loop and do while loop .
Functions
Function are the building block of statements, which takes some data, manipulate them and can return a value. Once a function has been written and debugged, it can be used again and again from the other part of the program. We can classify function in two groups. They are
Function
User defined functions.
Functions: – Library functions are build-in functions. There are ready made commands in javascript.
for example, date(), getdate(), sqrt() etc.
User defined functions : – The functions which are made by the user.
Example,
<html>
<Head><title> function in java</title>
</head>
<Script Language=”JavaScript”>
function hit()
{
document.write(“<center>”+”Hello world”);
document.write(“<br> “+” =========”);
}
function namrata()
{
document.write(“<center>”+”Hi Namrata”);
document.write(“<br>”+”==========”);
}
</script>
<form>
<input type =”button” name=”dd” value=”Hit” onClick=”hit()”>
<input type=”button” name=”d” value=”info” onClick=”namrata()”>
</form>
</html>
Thanks for support. If you like this content please comment.