JAVASCRIPT THEORY
JAVASCRIPT THEORY
JAVASCRIPT THEORY
<html>
<head>
</head>
<body>
<script>
document.getElementById("demo").innerHTML = "My First JavaScript";
</script>
</body>
</html>
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 a <script> tag:
<script src="myScript.js"></script>
EXTERNAL JAVASCRIPT
ADVANTAGES
Placing scripts in external files has some advantages:
To add several script files to one page - use several script tags:
JAVASCRIPT OUTPUT
JavaScript Display Possibilities
JavaScript can "display" data in different ways:
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:
USING DOCUMENT.WRITE()
For testing purposes, it is convenient to use document.write():
USING WINDOW.ALERT()
You can use an alert box to display data:
USING CONSOLE.LOG()
For debugging purposes, you can call the console.log() method in the browser
to display data.
PARSE FUNCTION:
The main purpose of using the parseInt function is to extract a number from a
string. This turns the returned value to an actual number. In the example above, 3 is
a string and not an actual number.
JAVASCRIPT PROGRAMS
▪ A computer program is a list of "instructions" to be "executed" by a
computer.
JAVASCRIPT STATEMENTS
▪ JavaScript statements are composed of:
SEMICOLONS ;
▪ Semicolons separate JavaScript statements.
JAVASCRIPT KEYWORDS
➢ JavaScript statements often start with a keyword to identify the
JavaScript action to be performed.
Keyword Description
JAVASCRIPT SYNTAX
JavaScript syntax is the set of rules, how JavaScript programs are constructed:
JavaScript Values
The JavaScript syntax defines two types of values:
• Fixed values
• Variable values
JAVASCRIPT LITERALS
The two most important syntax rules for fixed values are:
10.50
1001
"John Doe"
'John Doe'
JAVASCRIPT VARIABLES
In a programming language, variables are used to store data values.
JavaScript uses the keywords var, let and const to declare variables.
JAVASCRIPT OPERATORS
JavaScript uses arithmetic operators ( + - * / ) to compute values:
JAVASCRIPT EXPRESSIONS
An expression is a combination of values, variables, and operators, which
computes to a value.
JAVASCRIPT KEYWORDS
JavaScript keywords are used to identify actions to be performed.
JAVASCRIPT COMMENTS
Not all JavaScript statements are "executed".
The rules for legal names are the same in most programming languages.
NOTE
Numbers are not allowed as the first character in names.
JAVASCRIPT ARITHMETIC
OPERATORS
Arithmetic operators are used to perform arithmetic on numbers:
Operator Description
+ Addition
- Subtraction
* Multiplication
** Exponentiation (ES2016)
/ Division
++ Increment
-- Decrement
JAVASCRIPT ASSIGNMENT
OPERATORS
Assignment operators assign values to JavaScript variables.
Operator Example Same As
= 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
JAVASCRIPT COMPARISON
OPERATORS
Operator Description
== equal to
!= not equal
? ternary operator
Operator Description
&& logical and
|| logical or
! logical not
Conditional Statements
Very often when you write code, you want to perform different actions for
different decisions.
THE IF STATEMENT
Use the if statement to specify a block of JavaScript code to be executed if a
condition is true.
if (condition) {
// BLOCK OF CODE TO BE EXECUTED IF THE CONDITION IS TRUE
}
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
}
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
}
Statement 1 is executed (one time) before the execution of the code block.
Statement 3 is executed (every time) after the code block has been
executed.
SYNTAX
while (CONDITION) {
// code block to be executed
}
SYNTAX
do {
// code block to be executed
}
while (CONDITION);