Javascript Notes
Javascript Notes
DEVELOPMENT
CLIENT-SIDE SCRIPTING
Client Side Scripting
Why use client-side programming?
PHP already allows us to create dynamic web pages. Why also use
client-side scripting?
client-side scripting (JavaScript) benefits:
usability: can modify a page without having to post back to the server
(faster UI)
efficiency: can make small, quick changes to page without waiting for
server
event-driven: can respond to user actions like clicks and key presses
Why use client-side programming?
server-side programming (PHP) benefits:
security:
has access to server's private data; client can't see source code
compatibility: not subject to browser compatibility issues
power: can write files, open connections to servers, connect to databases, ...
TERMS USED: CLIENT SIDE AND
SERVER SIDE
9
WHAT IS JAVASCRIPT?
JavaScript can execute not only in the browser, but also on the server,
or actually on any device that has a special program called the
JavaScript engine.
The browser has an embedded engine sometimes called a “JavaScript
virtual machine”.
WHAT IS JAVASCRIPT?
..CONTD
The engine (embedded if it’s a browser) reads (“parses”) the
script. Then it converts (“compiles”) the script to machine
code.
And then the machine code runs, pretty fast.
The engine applies optimizations at each step of the process. It
even watches the compiled script as it runs, analyzes the data
that flows through it, and further optimizes the machine code
based on that knowledge.
12
WHAT IS JAVASCRIPT?
..CONTD
• In-browser JavaScript can do everything related to webpage manipulation,
interaction with the user, and the webserver.
• For instance, in-browser JavaScript is able to:
• Add new HTML to the page, change the existing content, modify styles.
• React to user actions, run on mouse clicks, pointer movements, key
presses.
• Send requests over the network to remote servers, download and upload
files.
• Get and set cookies, ask questions to the visitor, show messages.
• Remember the data on the client-side (“local storage”).
13
EXTERNAL JAVASCRIPT
app.js
15
STATEMENTS IN JAVASCRIPT
BLOCK
IDENTIFIERS
• An identifier is a name you choose for variables, parameters, functions, classes,
etc.
• An identifier name starts with a letter (a-z, or A-Z), an underscore(_), or a dollar
sign ($) and is followed by a sequence of characters including (a-z, A-Z),
numbers (0-9), underscores (_), and dollar signs ($).
• Identifiers in JavaScript are case-sensitive. For example, the message is different
from the Message.
18
COMMENTS
• Comments allow you to include notes or hints in JavaScript code. When executing the code, the
JavaScript engine ignores the comments.
• JavaScript supports both single-line and block comments.
• Single-line comments
• A single-line comment starts with two forward-slashes characters (//). It turns all the text following
the // on the same line into a comment. For example:
• // this is a single-line commentCode language: JSON / JSON with Comments (json)
• Block comments
• A delimited comment begins with a forward slash and asterisk /* and ends with the opposite */ as in
the following example:
• /* This is a block comment that can span multiple lines */
19
EXPRESSIONS
• An expression is a piece of code that evaluates to a value.
• For example: 2 + 1The above expression returns three.
20
VARIABLES
• A variable is a label that references a value like a number or string. Before using
a variable, you need to declare it.
Declaring a variable
• To declare a variable, you use the var keyword followed by the variable name as
follows:
var variableName;
VARIABLES
• In JavaScript, variable names follow these rules:
• Variable names are case-sensitive. This means that the message and Message are different
variables.
• Variable names can only contain letters, numbers, underscores, or dollar signs and
cannot contain spaces. Also, variable names must begin with a letter, an underscore (_) or
a dollar sign ($).
• Variable names cannot use the reserved words.
• By convention, variable names use camelCase like message, yourAge, and myName.
PRINTING “HELLO WORLD”-USING 23
CONSOLE
PRINTING “HELLO WORLD”-USING 24
DOCUMENT.WRITE
PRINTING “HELLO WORLD”-USING 25
DOCUMENT.WRITE
26
EXTERNAL SCRIPTS
If we have a lot of JavaScript code, we can put it into a separate file.
Script files are attached to HTML with the src attribute:
<script src="/path/to/script.js"></script>
Here, /path/to/script.js is an absolute path to the script from the site root. One can also
provide a relative path from the current page. For instance, src="script.js", just
like src="./script.js", would mean a file "script.js" in the current folder.
DIFFERENT WAYS:
• Writing into an HTML element, using innerHTML.
• Writing into the HTML output using document.write().
• Writing into an alert box, using window.alert().
• Writing into the browser console, using console.log().
PRINTING “HELLO WORLD”-USING 29
INNER HTML
CS380
30
1. The statement1 is executed first even before executing the looping code. So,
this statement is normally used to assign values to variables that will be used
inside the loop.
2. The statement2 is the condition to execute the loop.
3. The statement3 is executed every time after the looping code is executed
JAVASCRIPT: FOR LOOP
JAVASCRIPT: FOR…IN LOOP
Syntax:
Output:
JAVASCRIPT: WHILE LOOP
2. Anonymous functions
Syntax: Example:
JAVASCRIPT SELF-EXECUTING ANONYMOUS FUNCTIONS
Another common use of anonymous functions is to create self-executing
functions (also known as IIFE – Immediately Invoked Function
Expressions). These functions run immediately after they are defined.
Example:
JAVASCRIPT ANONYMOUS FUNCTION USING
ARROW FUNCTION TECHNIQUE
ES6 introduced a new and shorter way of declaring an anonymous function,
which is known as Arrow Functions. In an Arrow function, everything
remains the same, except here we don’t need the function keyword also.
Here, we define the function by a single parenthesis and then ‘=>’ followed
by the function body. Example:
JAVASCRIPT ANONYMOUS FUNCTION USING
ARROW FUNCTION TECHNIQUE
If we have only a single statement in the function body, we can even remove
the curly braces.
Example:
JAVASCRIPT SELF-EXECUTING ANONYMOUS
FUNCTION USING ARROW FUNCTION TECHNIQUE
Declaring a self-executing anonymous function (without the name itself)
and will see how we may declare it as well as how we may call it in order to
print the resultant value.
Example:
Syntax:
JAVASCRIPT DOM
When a web page is loaded, the browser creates a Document Object Model of the page.
The HTML DOM is a standard for how to get, change, add, or delete HTML elements.
The HTML DOM model is constructed as a tree of Objects.
EVENT-DRIVEN PROGRAMMING
EVENT-DRIVEN PROGRAMMING