Java Script Notes
Java Script Notes
This example uses the method to "find" an HTML element (with id="demo")
and changes the element content (innerHTML) to "Hello JavaScript":
<!DOCTYPE html>
<html>
<body>
<button type="button"
onclick='document.getElementById("demo").innerHTML = "Hello
JavaScript!"'>Click Me!</button>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p>In this case JavaScript changes the value of the src (source) attribute of
an image.</p>
<button
onclick="document.getElementById('myImage').src='pic_bulbon.gif'">Turn
on the light</button>
<button
onclick="document.getElementById('myImage').src='pic_bulboff.gif'">Turn
off the light</button>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<button type="button"
onclick="document.getElementById('demo').style.fontSize='3
5px'">Click Me!</button>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<button type="button"
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<button type="button"
onclick="document.getElementById('demo'). style.display='block'">Click
Me!</button>
</body>
</html>
**
The <script> Tag
In HTML, JavaScript code must be inserted between <script> and </script>
tags.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript in Body</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "My First JavaScript";
</script>
</body>
</html>
JavaScript in <head>
In this example, a JavaScript function is placed in the <head> section of an
HTML page.
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
document.getElementById("demo").innerHTML =
"Paragraph changed.";
}
</script>
</head>
<body>
<h2>JavaScript in Head</h2>
</body>
</html>
JavaScript in <body>
In this example, a JavaScript function is placed in the <body> section of an
HTML page.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript in Body</h2>
<script>
function myFunction() {
</script>
</body>
</html>
Using innerHTML
To access an HTML element, JavaScript can use
the document.getElementById(id) method.
<html>
<body>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 5 + 6;
</script>
</body>
</html>
Using document.write()
For testing purposes, it is convenient to use document.write():
<!DOCTYPE html>
<html>
<body>
<script>
document.write(5 + 6);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Using window.alert()
You can use an alert box to display data:
<!DOCTYPE html>
<html>
<body>
<script>
window.alert(5 + 6);
</script>
</body>
</html>
JavaScript Statements
Semicolons separate JavaScript statements.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Statements</h2>
<p id="demo"></p>
<script>
var x, y, z; // Statement 1
x = 5; // Statement 2
y = 6; // Statement 3
z = x + y; // Statement 4
document.getElementById("demo").innerHTML =
</body>
</html>
JavaScript Keywords
JavaScript statements often start with a keyword to identify the JavaScript
action to be performed.
Here is a list of some of the keywords you will learn about in this tutorial:
Keywor Description
d
debugge Stops the execution of JavaScript, and calls (if available) the
r debugging function
JavaScript Syntax
JavaScript syntax is the set of rules, how JavaScript programs are constructed:
JavaScript Variables
In a programming language, variables are used to store data values.
x = 6;
JavaScript Operators
JavaScript uses arithmetic operators ( + - * / ) to compute values:
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (Remainder)
++ Increment
-- Decrement
100 + 50
<!DOCTYPE html>
<html>
<body>
<h2>The + Operator</h2>
<p id="demo"></p>
<script>
var x = 5;
var y = 2;
var z = x + y;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html>
= 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
^= x ^= y x=x^y
|= x |= y x=x|y
**= x **= y x = x ** y
Function names can contain letters, digits, underscores, and dollar signs
(same rules as variables).
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Functions</h2>
<p>This example calls a function which performs a calculation, and returns the result:</p>
<p id="demo"></p>
<script>
return p1 * p2;
}
</script>
</body>
</html>
car.color = white
car.stop()
All cars have the same properties, but the property values differ from car
to car.
All cars have the same methods, but the methods are performed at
different times.
Objects are variables too. But objects can contain many values.
<html>
<body>
<h2>JavaScript Objects</h2>
<p id="demo"></p>
<script>
// Create an object:
</body>
</html>
What is "this"?
In a function definition, this refers to the "owner" of the function.
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
// Create an object:
var person = {
firstName: "John",
lastName : "Doe",
id : 5566,
fullName : function() {
};
document.getElementById("demo").innerHTML = person.fullName();
</script>
</body>
</html>
JavaScript Common Mistakes
<html>
<body>
<p id="demo"></p>
<script>
var x = 0;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var x = 10 + "5";
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
Perfect Output
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var x = 0.1;
var y = 0.2;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html>
Misplacing Semicolon
THANK YOU