Introduction To JavaScript - Finals
Introduction To JavaScript - Finals
Introduction To JavaScript - Finals
Example 2.
JavaScript is a programming language used to make <!DOCTYPE html>
<html>
web pages interactive. It runs on your visitor's <body>
computer and doesn't require constant downloads
from your website. JavaScript is often used to <h1>What Can JavaScript Do?</h1>
create polls and quizzes.
<p id="demo">JavaScript can change the
style of an HTML element.</p>
JavaScript and Java are completely different
languages, both in concept and design. JavaScript <button type="button"
onclick="myFunction()">Click Me!</button>
was invented by Brendan Eich in 1995, and became
an ECMA standard in 1997. ECMA-262 is the official <script>
name. ECMAScript 6 (released in June 2015) is the function myFunction() {
var x = document.getElementById("demo");
latest official version of JavaScript. x.style.fontSize = "25px";
x.style.color = "red";
ECMAScript (European Computer Manufacturers }
</script>
Association Script) - ECMAScript (or ES) is a
trademarked scripting-language specification </body>
</html>
standardized by ECMA International. Well-known
implementations of the language, such as Example 3:
JavaScript, JScript and ActionScript have come into <!DOCTYPE html>
wide use for client-side scripting on the Web. <html>
<body>
<button type="button"
1. JavaScript Can Change HTML Content onclick="myFunction()">Submit</button>
2. JavaScript Can Change HTML Attributes
3. JavaScript Can Change HTML Styles (CSS) <p id="demo"></p>
4. JavaScript Can Validate Data <script>
function myFunction() {
Sample Codes: var x, text;
Example 1:
// Get the value of the input field with
<!DOCTYPE html>
id="numb"
<html>
x = document.getElementById("numb").value;
<body>
// If x is Not a Number or less than one
<h1>What Can JavaScript Do?</h1>
or greater than 10
if (isNaN(x) || x < 1 || x > 10) {
<p id="demo">JavaScript can change HTML
text = "Input not valid";
content.</p>
} else {
text = "Input OK";
<button type="button"
}
onclick="document.getElementById('demo').i
nnerHTML = 'Hello JavaScript!'">
Click Me!</button> document.getElementById("demo").innerHTML
= text;
</body> }
</html> </script>
</body>
</html>
3. Using innerHTML
<!DOCTYPE html>
JavaScript Display Possibilities
<html>
<body>
JavaScript does NOT have any built-in print or
display functions. JavaScript can "display" data in
<h1>My First Web Page</h1>
different ways:
<p>My First Paragraph.</p>
1. Writing into an alert box, using
<p id="demo"></p>
window.alert().
2. Writing into the HTML output using
<script>
document.write().
document.getElementById("demo").innerHTML = 7
3. Writing into an HTML element, using
+ 6;
innerHTML.
</script>
4. Writing into the browser console, using
console.log().
</body>
</html>
Examples:
<h1>JavaScript Variables</h1>
<p>In this example, x, y, and z are variables</p> To assign a value to the variable, use the equal
<p id="demo"></p> sign:
ex: var carName = "Volvo";
<script>
var x = 5;
var y = 6; Example:
var z = x + y; <!DOCTYPE html>
document.getElementById("demo").innerHTML = z; <html>
</script> <body>
<script> </body>
var x = 5 + 2 + 3; </html>
document.getElementById("demo").innerHTML =
x;
</script>
</body>
</html>
Example 2:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Variables</h1>
<p id="demo"></p>
<script>
var x = "John" + " " + "Doe";
document.getElementById("demo").innerHTML =
x;
</script>
Arithmetic Operator
(+) - Addition
(-) - Subtraction
(*) - Multiplication
(/) - Divide
(%) - Modulus
(++) - Increment by 1
(--) - Decrement by 1
Comparison Operator
== - Equal
!= - not equal
> - greater than
< - less than
>= - greater than or equal
<= - less than or equal
Logical operator
&& - AND
|| - OR
! - NOT
Syntax:
If (expression) {
Statement(s) to be executed
if expression is true
}
Example:
JavaScript Data Types:
<!DOCTYPE html>
Numbers e.g. 123, 150, 160 <html>
String of text e.g. “this text string” <body>
Boolean e.g. true or false
<h1> Validate Input</h1>
JavaScript variable scope:
Global variables: A global variable has <p>input a number</p>
global scope w/c means it is defined <input id="numb">
everywhere in your JavaScript code.
Local variables: A local variable will be <button type="button"
visible only within a function where it is onclick="myFunction()">Click Me!</button>
defined.
<p id="demo"></p> function myFunction() {
<script> var x, text;
function myFunction() { // Get the value of the input field with id="numb"
var x, text; x = document.getElementById("numb").value;
// Get the value of the input field with id="numb" if (isNaN(x) || x <= 18) {
x = document.getElementById("numb").value; text = "too young";
} else {
text = "old enough";
if (isNaN(x) || x == 1) { }
text = "input is 1"; document.getElementById("demo").innerHTML
} = text + " to vote";
document.getElementById("demo").innerHTML }
= text; </script>
} </body>
</script> </html>
Syntax: if (expression1) {
If (expression) { Statement(s)
Statement(s) } else if (expression 2) {
} else { Statement(s)
Statement(s) } else {
} Statement(s)
}
Example:
Example:
<!DOCTYPE html>
<html> <!DOCTYPE html>
<body> <html>
<body>
<h1> Validate Input</h1>
<p>input your age</p> <h1> Validate Input</h1>
<p>input your age</p>
<input id="numb">
<input id="numb">
<button type="button"
onclick="myFunction()">Click Me!</button> <button type="button"
onclick="myFunction()">Click Me!</button>
<p id="demo"></p>
<script> <p id="demo"></p>
<script>
default: statement(s)
function myFunction() { }
var x, text;
if (isNaN(x) || x == 0) { Example:
text = "input invalid"; <!DOCTYPE html>
} <html>
else if (x <= 17) { <body>
text = "not eligible";
} <h1>JavaScript Can Validate Input</h1>
else{
text = "you are elible"; <p>input your age</p>
}
document.getElementById("demo").innerHTML <input id="numb">
= text + " to vote";
} <button type="button"
</script> onclick="myFunction()">Click Me!</button>