III- JAVA SCRIPT
JS
• Scripting languages
• JavaScript is used in millions of Web pages to add functionality,
validate forms, detect browsers, and much more.
• used to improve the design, validate forms.
• JavaScript is the most popular scripting language on the Internet, and
works in all major browsers, such as Internet Explorer, Mozilla Firefox,
and Opera
JS
• designed to add interactivity to HTML pages
• lightweight programming language
• JavaScript is usually embedded directly into HTML pages
• JavaScript is an interpreted language (means that scripts execute
without preliminary compilation)
• Open source
• Java and JavaScript are two completely different languages in both
concept and design
Contd.,
• JavaScript gives HTML designers a programming tool
• JavaScript can put dynamic text into an HTML page
• JavaScript can react to events
• JavaScript can read and write HTML elements –
• JavaScript can be used to validate data
• JavaScript can be used to detect the visitor's browser
• JavaScript can be used to create cookies
JS- variables
• "containers" for storing information.
• JavaScript variables are used to hold values or expressions
• A variable can have a short name or a more descriptive name,
Rules for JavaScript variable names:
• Variable names are case sensitive (y and Y are two different variables)
• Variable names must begin with a letter or the underscore character
Syntax:
var varaiblename;
Eg. var r; var fname;
var x=15;
var fname=“Jhon”;
If you assign values to variables that have not yet been declared, the
variables will automatically be declared.
Eg: var fname=“Jhon”;
Lname=“David”;
Redeclaring JavaScript Variables
• If you redeclare a JavaScript variable, it will not lose its original value.
var x=5; var x;
After the execution of the statements above, the variable x will still
have the value of 5. The value of x is not reset (or cleared) when you
redeclare it.
EXAMPLE
<body>
<script type="text/javascript">
var firstname;
firstname="Welcome";
[Link](firstname);
[Link]("<br />");
firstname="XYZ";
[Link](firstname);
</script>
<p>The script above declares a variable, assigns a value to it, displays the value,
change the value,
and displays the value again.</p> </body> </html>
Operators in Java Script
• javaScript Arithmetic Operators
• JavaScript Assignment Operators
• Comparison Operator
• Logical Operator
• Conditional Operator
•
JavaScript Arithmetic Operators
Operator Description Example Result
+ Addition X=y+5
- Subtraction X=y-7
* Multiplication X=y*6
/ Division X=y/3
% Modulus X=y%2
++ Increment X=y++
-- Decrement X=y--
JavaScript Assignment Operators
= X=Y X=Y
+= X+=Y X=X+Y
*= X*=Y X=X*Y
/+ X/=Y X=X/Y
%= X%=Y X=X%Y
-= X-=Y X=X-Y
+ OPERATOR IN STRING
WHEN ADD TWO STRING VALUES
txt1="What a very"; txt2="nice day"; txt3=txt1+txt2;
If you add a number and a string, the result will be a string.
JavaScript Comparison and Logical operators are used to test
for true or false.
COMPARISON OPERATOR
• Comparison operators are used in logical statements to determine
equality or difference between variables or values.
== Is equal to
=== Is exactly equals to
!= Not equal
> Is greater than
< Is lesser than
<= Greater than equal to
>= Less than equal to
Example
• if (age <18)
[Link](“Too young to vote”);
Logical Operators
&& And (X>5 && y<9)
|| OR
(X==5||Y==5)
! NOT !(X==Y) IS TRUE