JavaScript
JavaScript
PIYUSH MISHRA
This tutorial is about JavaScript, and how JavaScript works with HTML and CSS.
JavaScript in <head> or <body>
You can place any number of scripts in an HTML document.
External JavaScript
Scripts can also be placed in external files.
External scripts are practical when the same code is used in many different web
pages.
To use an external script, put the name of the script file in the src (source)
attribute of the <script> tag:
<script src="first.js"></script>
External JavaScript Advantages
Placing Java Scripts in external files has some advantages:
Strings are written inside double or single quotes. Numbers are written without
quotes.
If you put quotes around a number, it will be treated as a text string.
Ex.
var pi = 3.14;
var person = "John Doe";
JavaScript Operators
JavaScript Arithmetic Operators
Arithmetic operators perform arithmetic on numbers (literals or variables).
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
++ Increment
-- Decrement
var x = a + b;
or expressions:
100 + 50
Adding
var x = 5;
var y = 2;
var z = x + y;
Output z=7;
The subtraction operator (-) subtracts numbers.
Subtracting
var x = 5;
var y = 2;
var z = x - y;
Output z=3;
x = x + 5
In JavaScript however, it makes perfect sense: It assigns the value of x + 5 to x.
(It calculates the value of x + 5 and puts the result into x. The value of x is
incremented by 5)
Comparison Operators
Comparison operators are used in logical statements to determine equality or
difference between variables or values.
Given that x=5, the table below explains the comparison operators:
x == 5 true
x === 5 true
Logical Operators
Logical operators are used to determine the
logic between variables or values.
Given that x=6 and y=3, the table below explains the logical operators:
|| or (x == 5 || y == 5) is false
JavaScript Arrays
JavaScript arrays are used to store multiple
values in a single variable.
var cars =
["Maruti", "Volvo", "BMW"];
What is an Array?
An array is a special variable, which can
hold more than one value at a time.
If you have a list of items (a list of car
names, for example), storing the cars in
single variables could look like this:
var car1 = "Saab";
var car2 = "Volvo";
var car3 = "BMW";
var array-name =
[item1, item2, ...];
var person =
{firstName:"John",
lastName:"Doe", age:46};
var a=cars.indexOf("Volvo");
document.write(a);
JavaScript Events
HTML events are "things" that happen to HTML
elements.
When JavaScript is used in HTML pages, JavaScript
can "react" on these events.
HTML Events
An HTML event can be something the browser does, or
something a user does.
<some-HTML-element some-
event='some JavaScript'>
Common HTML Events
Here is a list of some common HTML events:
Event Description
Page
JavaScript If...Else Statements
Syntax
if (condition) {
block of code to be executed
if the condition is true
}
Example
Make a "Good day" greeting if the time is less than 20:00:
if (age<= 18) {
greeting = "Good day";
}
Syntax
if (condition1) {
block of code to be executed
if condition1 is true
} else if (condition2) {
block of code to be executed
if the condition1 is false and
condition2 is true
} else {
block of code to be executed
if the condition1 is false and
condition2 is false
}
Example
If time is less than 10:00, create a "Good morning" greeting, if not, but time is
less than 20:00, create a "Good day" greeting, otherwise a "Good evening":
if (time < 10) {
greeting = "Good morning";
} else if (time < 20) {
greeting = "Good day";
} else {
greeting = "Good evening";
}