CSE-326-21-Java Scripts 1

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 43

Java Scripts

Introduction
Change the HTML Content (getElementById( ))
Introduction
Change the HTML Content (getElementById( ))
Introduction
Change HTML attribute values
Introduction
Change HTML attribute values
Introduction
Change HTML attribute values
Introduction
Change HTML Style (CSS)
Introduction
Can control what to show / hide? (display style property)
Where to put JavaScript?
The <script> tag
Where to put JavaScript?
The <script> tag inside <head>
Where to put JavaScript?
The <script> tag inside <head> or <body>

Placing scripts at the bottom of the <body> element


improves the display speed, because script interpretation
slows down the display.
Where to put JavaScript?
Linking a External Script File
 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:
Where to put JavaScript?
Benefits of External Java Scripts

Placing scripts in external files has some advantages:

It separates HTML and code


It makes HTML and JavaScript easier to read and maintain
Cached JavaScript files can speed up page loads
Where to put JavaScript?
How to refer external java scripts?

An external script can be referenced in 3 different ways:

With a full URL (a full web address)


With a file path (like /js/)
Without any path
This example uses a full URL to link to myScript.js:
Where to put JavaScript?
Refer external java scripts using path

Using File Path

Using No Path (Same directory as project)


Output in JavaScript
Using innerHTML (Write to an element)

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:
Output in JavaScript
Using document.write( ) used for testing purpose
Output in JavaScript
Using document.write( ) after loading complete page
Output in JavaScript
Using window.alert( ) – use alert box for display output
 You can skip the window keyword.
 In JavaScript, the window object is the global
scope object.
 This means that variables, properties, and
methods by default belong to the window
object.
 This also means that specifying
the window keyword is optional:
Output in JavaScript
Using window.alert( ) – use alert box for display output

 Open the page and then open the debugger in


browser
 Then click on console to see the output
Statements in JavaScript
How to declare variable?

JavaScript statements are composed of:


 Values
Operators
Expressions
Keywords, and
Comments.
 The statements are executed, one by one, in the same order as they are
written.
 Semicolons separate JavaScript statements.
Statements in JavaScript
Length of a statement

For best readability, programmers often like to avoid code lines longer
than 80 characters.
If a JavaScript statement does not fit on one line, the best place to break it
is after an operator:
Statements in JavaScript
Keywords
JavaScript Syntax
Declaring variable

It is not compulsory to end a statement with


semi-colon. But considered as good practice
 var and let both keywords can be used to
declare a variable

When to prefer which one will be discussed later in scope rules.


 JavaScripts doesn’t considers the whitespace surrounding the code.
You can add whenever it is necessary to improve code readability.
JavaScript Syntax
Types of variables and comments
const keyword is used to create a
constant and no further updating is
allowed
 A single-line comments can be
added to the code using two front-
slash (//)
JavaScript Syntax
Types of values

The JavaScript syntax defines two types of values:


Fixed values
Variable values
Fixed values are called Literals.
Variable values are called Variables.
JavaScript Types

 Number
 Without Decimal-point, Integers (upto 15 digits)
 With Decimal-point Floating-point (64 bit)
NaN
 String
 Booleans
 Function
Array
Dates
JavaScript Operators
Arithmetic Operators
JavaScript Operators
Assignment Operators
JavaScript String

Adding string with number


JavaScript Operators
Comparison Operators
JavaScript Operators
Comparison Operators (equal value and type)
JavaScript Operators
Logical Operators
 Logical operators are used to determine the logic between variables or values.
 Given that x = 6 and y = 3, the table below explains the logical operators:
JavaScript Operators
Example Logical Operators
JavaScript Array
Declaring an array
JavaScript Array
Array Methods

Method Name Description


toString() converts an array to a string of (comma separated) array values.
join()  joins all array elements into a string
Pop() Removes the last element
Push() Adds an element at last and returns the new length of the array
Shift() Removes the first element and shift other items to lower index and returns the shift out item
Unshift() Adds an element to first and returns the new length
Concat() Creates a new array by merging (concatenating) existing arrays.
Can also add an item to the array and also returns the new array.
splice() add new items to an array
slice() remove elements without leaving "holes" in the array
JavaScript Loops
Types of Loops

JavaScript supports different kinds of loops:


•for - loops through a block of code a number of times
•for/in - loops through the properties of an object
•for/of - loops through the values of an iterable object
•while - loops through a block of code while a specified condition is true
•do/while - also loops through a block of code while a specified condition is true
JavaScript Loops
For loop

Find the sum of all


JavaScript Loops
while loop – array input
JavaScript Loops
do while loop – This loop will execute the code block once, before
checking if the condition is true
JavaScript Type
Object – contain many values, car is an object
JavaScript Loops
For in loop – used to loop through the properties of an Object
JavaScript Loops
Practice Questions

 Find the maximum and minimum of all the items


 Add item to an array as user input
 Sort the items in ascending and descending order
 Delete an item from array as user input
 Count how many are even and odd numbers

You might also like