Ict Reviewer 3RD QTR
Ict Reviewer 3RD QTR
HARMONY
External JavaScript Advantages
codename of planned new release (ES6)
It separates HTML and code
It makes HTML and JavaScript easier to read
getElementById() and maintain
JavaScript can change HTML Content Cached JavaScript files can speed up page loads
one of many JavaScript HTML Method
The example below “finds” an HTML element
JavaScript Output
(with id=”demo”), and changes the element content
JavaScript can “display” data in different ways
(innerHTML) to “Hello JavaScript”
innerHTML
Example:
document.getElementById(“demo”).innerHTML = “Hello
document.write()
JavaScript”; window.alert()
console.log()
JavaScript accepts both double and
single quotes
Keyword Description
JavaScript Print break terminates a switch or loop
JavaScript does not have any print object or function declares a function
print methods. Return exits a function
Var declares a variable
The only exception is that you can call the for marks a block of statements,
and repeats the block, while the
window.print() method in the browser to print the condition is true
content of the current window.
JavaScript Syntax
JavaScript Programs JavaScript syntax is the set of rules
A computer program is a list of “instructions”
to be “executed” by a computer. JavaScript Values
In a programming language, these programming The JavaScript syntax defines two types of
instructions are called statements. values: fixed values (literals) & variable values
A JavaScript program is a list of programming (variables)
statements.
In HTML, JavaScript programs are executed by
TWO MOST IMPORTANT SYNTAX RULES
the web browser.
FOR FIXED VALUES (LITERALS)
Numbers are written with or without decimals.
JavaScript Statements
Strings are text, written within double or single
are composed of Values, Operators,
quotes
Expressions, Keywords, and Comments.
JavaScript programs (and JavaScript
statements) are often called JavaScript code.
JavaScript Variables
Variables are used to store data values
JavaScript uses the var keyword to declare
Semicolons (;) variables
Semicolons separate JavaScript statements An equal sign is used to assign values to
Add a semicolon at the end of each statement. variables.
JavaScript Operators JavaScript Character Set
JavaScript uses arithmetic operators (+ - * /) JavaScript uses the Unicode character set.
to compute values Unicode covers (almost) all the characters,
JavaScript uses an assignment operator (=) to punctuations, and symbols in the world.
assign values to variables.
JavaScript Comments
JavaScript Expression
An expression is a combination of values, Single Line Comments
variables and operators, which computes to a it starts with //
value. Any text between // and the end of the line
The computation is called an evaluation. will be ignored by JavaScript
Expressions can also contain variable values
Multi-Line Comments
JavaScript Keywords Multi-Line comments start with /* and end
with */
JavaScript keywords are used to identify
Any text between /* and */ will be ignored by
actions to be performed.
JavaScript.
The var keyword tells the browser to create
variables. Using Comments to Prevent Execution
suitable for code testing
JavaScript Comments Adding // in front of a code line changes the
Code after double slashes // or between /* and code lines from an executable line to a comment.
*/ is treated as a comment
Comments are ignored, and will not be JavaScript Function Definitions
executed. JavaScript functions are defined with the
function keyword.
JavaScript Identifiers You can use a function declaration or a
Identifiers are names function expression
In JavaScript, identifier are used to name
variables (and keywords, and functions, and Functions are declared with the following
labels) syntax:
The rules for legal names are much the same
in most programming languages function functionName(parameters) {
In JavaScript, the first character must be a // code to be executed
letter, or an underscore, or a dollar sign ($) }
Subsequent characters may be letters, digits,
underscores, or dollar signs Declared functions are not executed
Numbers are not allowed as the first character, immediately. They are “saved for later use”, and
this way JavaScript can easily distinguish will be executed later, when they are invoked
indentifiers from numbers. (called upon)
function myFunction(a, b) {
JavaScript and Camel Case
return a * b;
Historically, programmers have used different
}
ways of joining multiple words into one variable
name.
A function declaration is not an executable
Hyphens (first-name, last-name) statement, it is not common to end it with a
Underscore (first_name, last_name) semicolon.
Upper Camel Case (FirstName, LastName)
Lower Camel Case (firstName, lastName)
Function Expressions Arrow Functions
A JavaScript function can also be defined using allows a short syntax for writing
an expression. function expressions.
A function expression can be stored in a variable You don’t need the function keyword, the
return keyword, and the curly brackets.
The Function () Constructor Arrow functions do not have their own
Functions can also be defined with a built-in this. They are not well suited for defining
JavaScript function constructor called function() object methods.
Arrow functions are not hoisted. They
Function Hoisting must be defined before they are used.
Using const is safer than using var,
Hoisting is JavaScript’s default behaviour of
because a function expression is always
moving declarations to the top of the current
constant value.
scope
You can only omit the return keyword
Hoisting applies to variable declarations and to
and the curly brackets if the function is a
function declarations
single statement.
JavaScript functions can be called before they
are declared.
JavaScript Function Parameters
Self-Invoking Functions and Arguments
Function expressions can be made “self- Function parameters are the names
invoking” listed in the function definition.
A self-invoking expression is invoked (started) Function arguments are the real values
automatically, without being called. passed to (and received by) the function
Function expressions will execute automatically,
without being called Parameter Rules
Function expression will execute automatically if JavaScript function definitions do not
the expression is followed by () specify data types for parameters
You cannot self-invoke a function declaration JavaScript functions do not perform type
You have to add parentheses around the checking on the passed arguments.
function to indicate that it is a function expression. JavaScript functions do not check the
number of arguments received.
Functions can be used as Values
JavaScript functions can be used in Expressions
Default Parameters
Functions are Objects If a function is called with missing arguments
The typeof operator in JavaScript returns (less than declared), the missing values are set to
“function” for functions. undefined.
But, JavaScript functions can best be Sometimes this is acceptable, but sometimes it
described as objects is better to assign a default value to the parameter.
JavaScript functions have both properties and
methods The Arguments Object
The arguments.length property returns the JavaScript functions have a built-in object
number of arguments received when the function called the arguments object
was invoked. it contains an array of the arguments used
when the function is invoked (called)
The toString() method returns the function as a This way you can simply use a function to find
string. (for instance) the highest value in a list of numbers
A Counter Dilemma
Suppose you want to use a variable for counting
something, and you want this counter to be
available to all functions. You could use a global
variable and a function to increase the counter
Any code on the page can change the counter,
without calling add(). The counter should be local
to the add() function, to prevent other code from
changing it.