Introduction To Javascript
Introduction To Javascript
</body>
</html>
It is a good idea to place scripts at the bottom of the
<body> element.
This can improve page load, because script compilation
can slow down the display.
EXTERNAL JAVASCRIPT
Scripts can also be placed in external files:
myScript.js
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
External scripts are practical when the same code is used in many different
web pages.
JavaScript files have the file extension .js.
To use an external script, put the name of the script file in the src (source)
attribute of a <script> tag:
Example
<!DOCTYPE html>
<html>
<body>
<script src="myScript.js"></script>
</body>
</html>
EXTERNAL JAVASCRIPT
You can place an external script reference in <head> or <body>
as you like.
The script will behave as if it was located exactly where the
<script> tag is located.
External scripts cannot contain <script> tags.
<!DOCTYPE html>
<html>
<body>
<script>
window.alert(5 + 6);
</script>
</body>
</html>
USING DOCUMENT.WRITE()
For testing purposes, it is convenient to use document.write():
Example
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<script>
document.write(5 + 6);
</script>
</body>
</html>
Using document.write() after an HTML document is fully loaded,
will delete all existing HTML:
<button onclick="document.write(5 + 6)">Try it</button>
USING INNERHTML
To access an HTML element, JavaScript can use the
document.getElementById(id) method.
The id attribute defines the HTML element. The innerHTML
property defines the HTML content:
Example
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My First Paragraph</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 5 + 6;
</script>
</body>
</html>
USING CONSOLE.LOG()
In your browser, you can use the console.log() method to display
data.
Activate the browser console with F12, and select "Console" in
the menu.
Example
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<script>
console.log(5 + 6);
</script>
</body>
</html>
JAVASCRIPT SYNTAX
JavaScript syntax is the set of rules, how JavaScript programs
are constructed
JavaScript Programs.
A computer program is a list of "instructions" to be "executed"
by the computer.
In a programming language, these program instructions are
called statements.
JavaScript is a programming language.
Example
var x = 5;
var y = 6;
var z = x + y;
JAVASCRIPT SYNTAX
JavaScript Statements
JavaScript statements are composed of:
Values, Operators, Expressions, Keywords, and Comments.
JavaScript Values
The JavaScript syntax defines two types of values: Fixed values and
variable values.
Fixed values are called literals. Variable values are called variables.
JavaScript Literals
The most important rules for writing fixed values are:
Numbers are written with or without decimals:
10.50
1001
Strings are text, written within double or single quotes:
"John Doe"
'John Doe'
JAVASCRIPT SYNTAX
JavaScript Variables
In a programming language, variables are used to store data values.
JavaScript uses the var keyword to declare variables.
An equal sign is used to assign values to variables.
In this example, x is defined as a variable. Then, x is assigned (given) the
value 6:
var x;
x = 6;
JavaScript Operators
JavaScript uses an assignment operator ( = ) to assign values to variables:
var x = 5;
var y = 6;
JavaScript uses arithmetic operators ( + - * / ) to compute values:
(5 + 6) * 10
JAVASCRIPT SYNTAX
JavaScript Expressions
An expression is a combination of values, variables, and operators, which
computes to a value.
The computation is called an evaluation.
For example, 5 * 10 evaluates to 50:
5 * 10
Expressions can also contain variable values:
x * 10
The values can be of various types, such as numbers and strings.
For example, "John" + " " + "Doe", evaluates to "John Doe":
JavaScript Keywords
JavaScript keywords are used to identify actions to be performed.
The var keyword tells the browser to create a new variable:
var x = 5 + 6;
var y = x * 10;
JAVASCRIPT SYNTAX
JavaScript Comments
Not all JavaScript statements are "executed".
Underscore:
Camel Case:
document.getElementById("demo").innerHTML =
"Hello Dolly.";
JAVASCRIPT STATEMENTS
JavaScript Code Blocks
JavaScript statements can be grouped together in code blocks,
inside curly brackets {...}.
The purpose of code blocks is to define statements to be
executed together.
One place you will find statements grouped together in blocks,
are in JavaScript functions:
Example
function myFunction() {
document.getElementById("demo").innerHTML = "Hello
Dolly.";
document.getElementById("myDIV").innerHTML = "How
are you?";
}
JAVASCRIPT STATEMENTS
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:
Keyword Description
debugger Stops the execution of JavaScript, and calls (if available) the debugging function
do ... while Executes a block of statements, and repeats the block, while a condition is true
Any text between // and the end of the line, will be ignored by
JavaScript (will not be executed).
Example
Example
var x = 5;
var y = 6;
var z = x + y;
All JavaScript variables must be identified with unique
names.
These unique names are called identifiers.
JAVASCRIPT IDENTIFIERS
Identifiers can be short names (like x and y), or more
descriptive names (age, sum, totalVolume).
The general rules for constructing names for variables (unique
identifiers) are:
Names can contain letters, digits, underscores, and dollar
signs.
Names must begin with a letter
Names can also begin with $ and _ (but we will not use it in
this tutorial)
Names are case sensitive (y and Y are different variables)
JavaScript can handle many types of data, but for now, just think
of numbers and strings.
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.
Example
var pi = 3.14;
var person = "John Doe";
var answer = 'Yes I am!';
DECLARING (CREATING) JAVASCRIPT
VARIABLES
Creating a variable in JavaScript is called "declaring" a
variable.
You declare a JavaScript variable with the var keyword:
var carName;
After the declaration, the variable has no value. (Technically
it has the value of undefined)
To assign a value to the variable, use the equal sign:
carName = "Volvo";
You can also assign a value to the variable when you declare
it:
var carName = "Volvo";
var carName;
RE-DECLARING JAVASCRIPT VARIABLES
var x = 5 + 2 + 3;
You can also add strings, but strings will be concatenated
(added end-to-end):
Example
var x = "Volvo" + 16 + 4;
JAVASCRIPT HAS DYNAMIC TYPES
Example
var carName = "Volvo XC60"; // Using double quotes
var carName = 'Volvo XC60'; // Using single quotes
JAVASCRIPT NUMBERS
Example
var x1 = 34.00; // Written with decimals
var x2 = 34; // Written without decimals
JAVASCRIPT BOOLEANS
Booleans can only have two values: true or false.
Example
var x = true;
var y = false;
JAVASCRIPT ARRAYS
document.getElementById("demo").innerHTML = myFunction(4,
3);
FUNCTION INVOCATION
The code inside the function will execute when
"something" invokes (calls) the function:
When an event occurs (when a user clicks a button)
When it is invoked (called) from JavaScript code
Automatically (self invoked)
if (condition) {
block of code to be executed if the condition is true
}
EXAMPLE
<!DOCTYPE html>
<html>
<body>
<p>Display “Not Allowed!" if the age is less than 18</p>
<p id=“ag"></p>
<script>
Var age=14;
if (age < 18) {
document.getElementById(“ag").innerHTML = “Not Allowed!";
}
</script>
</body>
THE ELSE STATEMENT
if (condition) {
block of code to be executed if the condition is true
} else {
block of code to be executed if the condition is false
}
EXAMPLE
<!DOCTYPE html>
<html>
<body>
<p>Display “Not Allowed!" if the age is less than 18 else print Allowed</p>
<p id=“ag"></p>
<script>
Var age=14;
if (age < 18) {
document.getElementById(“ag").innerHTML = “Not Allowed!";
}
else{
document.getElementById(“ag").innerHTML = “Allowed!";
}
</script>
</body>
THE ELSE IF STATEMENT
Use the else if statement to specify a new condition if the
first condition is false.
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
<!DOCTYPE html>
<html>
<body>
<p> Comparing Numbers</p>
<p id=“comp"></p>
<script>
Var x=14;
Var y=14;
if (x > y) {
document.getElementById(“comp").innerHTML =x+ “is gratest";
}
else if (y> x) {
document.getElementById(“comp").innerHTML =y+ “is gratest";
}
else {
document.getElementById(“comp").innerHTML = “both are equal";
}
</script>
</body>
THE JAVASCRIPT SWITCH STATEMENT
The switch statement is used to perform different actions based on different
conditions.
Use the switch statement to select one of many blocks of code to be executed.
Syntax
switch(expression) {
case n:
code block
break;
case n:
code block
break;
default:
default code block
}
This is how it works:
The switch expression is evaluated once.
The value of the expression is compared with the values of each case.
If there is a match, the associated block of code is executed.
THE BREAK KEYWORD
When the JavaScript code interpreter reaches
a break keyword, it breaks out of the switch block.
This will stop the execution of more code and case testing
inside the block.
The default Keyword
The default keyword specifies the code to run if there is no
case match:
EXAMPLE
<!DOCTYPE html>
<html>
<body>
<input id="myInput" type="text" value="Tutti Frutti">
<button onclick="checkFruit()">Check Fruit</button>
<p id="demo"></p>
<script>
function checkFruit() {
var text;
var fruits = document.getElementById("myInput").value;
switch(fruits) {
case "Banana":
text = "Banana is good!";
break;
case "Orange":
text = "I am not a fan of orange.";
break;
case "Apple":
text = "How you like them apples?";
break;
// add the default keyword here
}
document.getElementById("demo").innerHTML = text;
}
</script>
</body>