Study Java Script Easy
JavaScript is a scripting language,designed to add interactivity to an html page.
The official name of javascript is ECMA script .ECMA-262 is the official JavaScript standard.The standard is based on JavaScript (Netscape) and JScript (Microsoft).
How to put javascript into an HTML,??
To insert a JavaScript into an HTML page, we use the <script> tag.Inside the
<script> tag we use the type attribute to define the scripting language.
Eg:1
<body>
<script type=”text/javascript”>
document.write(“Hello World!”);
</script>
</body>
JavaScripts can be put in <head> section or <body> also.JavaScripts in the body part will be executed onlt when the page loads.JavaScripts in the head section will be executed only when CALLED.
Eg:2( Describing JS in <head> section)
<html>
<head>
<script type=”text/javascript”>
function message()
{
alert(“This alert box was called with the onload event”);
}
</script>
</head>
<body onload=”message()”>
</body>
</html>
Eg:3 (Describing JS in <body> portion)
same as Eg:1
Script can be put in both head and body sections.
Comments in JS is used to increase readability of the code.Single line comments use //.
Eg4:(Use of single line comments)
<script type=”text/javascript”>
// This will write a header:
document.write(“<h1>This is a header</h1>”);
</script>
For multi line commenting (/* */ ) is used.
Variables in JavaScript
JavaScript variables are used to hold values or expressions.Naming conventions are normal.Variable names are case sensitive (a and A are different).Variable names must begin with a letter or the underscore character.
Declaring variable in JavaScript
You can declare JavaScript variables with the ‘var’ statement:
Eg:
var x;
var sample;
Redeclaring a JavaScript variable, will not lose its original value.
Eg:
var a=5;
var a;
The redeclaration of ‘a’ wont lose its value ’5′ in the above Eg.
Operators in JavaScript
The operator = is used to assign values, + is used to add values,% is for modulus operation,++ for increment operation,– for decrement operation.
Comparison Operators:
<,>,<=,>=,==,!= etc are as usual
A new thing is “===”,it means exactly equal to value and type.
I will explain this with an
Eg:
x=3;
x===3 is true.
x===”3″ is false.
Eg.5(Use of conditional operator)
if (age<21)
document.write(“Cant buy this”);
Logical Operators:
&&,||,! are same as that of other languages.
Conditional Operator:
This assigns a value to a variable based on a condition.
Syntax:variablename=(condition)?value1:value2
Eg:6(if statement)Simply copy this to a notepad save it as an html and open in your browser.
<html>
<body>
<script type=”text/javascript”>
var d = new Date();
var time = d.getHours();
var time1=d.getMinutes();
if (time < 12 && time1 < 50 )
{
document.write(“<b>Good morning</b>”);
}
else
{
document.write(“<t>Time is past 12.50</t>”);
}
</script>
<p>
This example demonstrates the If statement.
</p>
<p>
If the time on your browser is less than 12,
you will get a “Good morning” greeting.
</p>
</body>
</html>
Notes:
1) It is case sensitive.
2) Semicolon is optional in javascript.
3) It uses curly braces to seperate blocks of code.
4)If you add a number and a string, the result will be a string.
Goto Next
Related posts:








it went resourcefull thanks
Leave your response!