JavaScript - Basics
JavaScript - Basics
Introduction
JavaScript
Example
Add HTML
Add CSS
JavaScript
Example
A very common use of JavaScript is to dynamically modify HTML and CSS to update a
Note that the code in your web documents is generally loaded and executed in the order
Errors may occur if JavaScript is loaded and run before the HTML and CSS that it is
intended to modify.
JavaScript
When the browser encounters a block of JavaScript, it generally runs it in order, from top
to bottom.
Here we are selecting a text paragraph (line 1), then attaching an event
listener to it (line 3) so that when the paragraph is clicked, the
updateName() code block (lines 5–8) is run.
JavaScript
In interpreted languages, the code is run from top to bottom and the result of running
the code is immediately returned. You don't have to transform the code into a different
Compiled languages on the other hand are transformed (compiled) into another form
before they are run by the computer. For example, C/C++ are compiled into machine
The program is executed from a binary format, which was generated from the original
The example below "finds" an HTML element (with id="demo"), and changes the
document.getElementById("demo").innerHTML
document.getElementById("demo").innerHTML == "Hello
"Hello JavaScript";
JavaScript";
In this example JavaScript changes the value of the src (source) attribute of an <img>
tag:
JavaScript
document.getElementById("demo").style.fontSize
document.getElementById("demo").style.fontSize == "35px";
"35px";
JavaScript
document.getElementById("demo").style.display
document.getElementById("demo").style.display == "none";
"none";
JavaScript
Showing hidden HTML elements can also be done by changing the display style:
document.getElementById("demo").style.display
document.getElementById("demo").style.display == "block";
"block";
Where is JavaScript Placed?
JavaScript
1. Internal JavaScript
Head Section
Scripts can be placed in the <body>, or in the <head> section of an HTML page, or
<!DOCTYPE
in both.
<!DOCTYPE html>
html>
<html>
<html>
<head>
<head>
<script>
<script>
function
function myFunction()
myFunction() {{
document.getElementById("demo").innerHTML
document.getElementById("demo").innerHTML == "Paragraph
"Paragraph
changed.";
changed.";
}}
</script>
</script>
</head>
</head>
</html>
</html>
JavaScript
Internal JavaScript
Body Section
Scripts can be placed in the <body>, or in the <head> section of an HTML page, or
<!DOCTYPE
in both.
<!DOCTYPE html>
html>
<html>
<html>
<body>
<body>
<h1>A
<h1>A Web
Web Page</h1>
Page</h1>
<p
<p id="demo">A
id="demo">A Paragraph</p>
Paragraph</p>
<button
<button type="button"
type="button" onclick="myFunction()">Try
onclick="myFunction()">Try
it</button>
it</button>
</body>
</body>
</html
</html>>
JavaScript
External JavaScript
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
<script src="myScript.js"></script>
src="myScript.js"></script>
JavaScript
External JavaScript
External JavaScript
To add several script files to one page - use several script tags:
JavaScript
External References
Go to your test site and create a new folder named scripts. Within the scripts folder,
In your index.html file, enter this code on a new line, just before the closing </body> tag
Activity
Make sure the HTML and JavaScript files are saved. Then load index.html in your
browser.
Activity
Example 2
<script>
<script>
document.getElementById("demo").innerHTML
document.getElementById("demo").innerHTML == "My
"My First
First
JavaScript";
JavaScript";
</script>
</script>
JavaScript Output
JavaScript
1. Using innerHTML
document.getElementById(id) method.
The id attribute defines the HTML element. The innerHTML property defines the
HTML content:
JavaScript
2. Using document.write()
2. Using document.write()
Using document.write() after an HTML document is loaded, will delete all existing HTML:
JavaScript
3. Using window.alert()
4. Using Console.log()
For debugging purposes, you can call the console.log() method in the browser to
display data.
JavaScript
5. Using window.print()
The only exception is that you can call the window.print() method in the browser
JavaScript Statements
This statement tells the browser to write "Hello Dolly." inside an HTML element
with id="demo":
JavaScript
JavaScript Variables
JavaScript uses the keywords var, let and const to declare variables.
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Variables#an_aside_on_variable_naming_rules
JavaScript
JavaScript Variables
Numbers
Strings
JavaScript
JavaScript Variables
Booleans
JavaScript
JavaScript Variables
let
JavaScript variables
Declaring a variable
Var score
Assigning a value to a variable
Var score = 0
JavaScript variables
Reassigning a variable
JavaScript variables
Const
You can use const keyword to declare variable in JavaScript
Eg The example below uses const to declare a variable
The output on the console will show the length of the string
The property (ie length) of the object (passphrase) is accessed using a dot (.)
JavaScript variables
Transform and manipulate a string
A method is an action you can perform with the object
JavaScript provides methods for strings eg
Example:
Output
Prompt()
A way to collect information from a site user
Output
JavaScript variables
Capture Input
Prompt()
A way to collect information from a site user
Output
JavaScript variables
Combine Strings
Concatenation of strings
A way to combine strings
JavaScript variables
Combine Strings
Template Literals
Template literals are literals delimited with backtick characters, allowing for multi-line strings and
string interpolation with embedded expressions
Replacement of concatenation
Example JS page
JavaScript variables
Display the Value of a String on a Page
Example using query selector to display string on a page
The Output
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
JavaScript variables
Find more string properties and methods
Strings
https://developer.mozilla.org/en-US/docs/Web/JavaScript
JavaScript Conditionals
JavaScript Conditionals
Display the Value of a String on a Page
If else
Replacement of concatenation
JavaScript Conditionals
Boolean
Boolean Values
a Boolean is a logical data type that can have only the values true or false
JavaScript Conditionals
Unary Plus
Unary Plus
The unary plus (+) operator precedes its operand and evaluates to its operand but attempts to
convert it into a number, if it isn't already.
JavaScript Conditionals
Conditional Statements
Example
Final code:
JavaScript Conditionals
Conditional Statements
Example if else
Final code:
JavaScript Conditionals
Display the Value of a String on a Page
If else
Replacement of concatenation
JavaScript Arrays
JavaScript
Array
An array is a single object that contains multiple values enclosed in square brackets and
separated by commas
Accessing Array
Once these arrays are defined, you can access each value by their location within the
array.
JavaScript
Length of Array
You can find out the length of an array (how many items are in it) using the length
property
JavaScript
Associative Array
Arrays with named indexes are called associative arrays (or hashes).
Function
Function Return
When JavaScript reaches a return statement, the function will stop executing.
If the function was invoked from a statement, JavaScript will "return" to execute the code
Functions often compute a return value. The return value is "returned" back to the
"caller":
JavaScript Functions
Why Functions?
You can reuse code: Define the code once, and use it many times.
You can use the same code many times with different arguments, to produce different
results.
JavaScript Functions
Why Functions?
Accessing a function without () will return the function object instead of the function
result.
JavaScript Functions
Why Functions?
Objects
Creating
To retrieve the information stored in the object, you can use the following syntax:
JavaScript
Objects
Creating
JavaScript
Accessing Objects
1. Dot Notation
Sub-Name Space
It is even possible to make the value of an object member another object. For example,
Sub-Name Space
To access these items you just need to chain the extra step onto the end with another
dot.
JavaScript
2. Bracket Notation
Objects
This keyword
The this keyword refers to the current object the code is being written inside
In the example above, this is the person object that "owns" the fullName function.
This keyword