SlideShare a Scribd company logo
JavaScript
JavaScript, often abbreviated as JS, is a programming
language that is one of the core technologies of the World
Wide Web, alongside HTML and CSS. As of 2022, 98%
of websites use JavaScript on the client side for webpage
behaviour, often incorporating third-party libraries.
A high-level definition
 JavaScript is a scripting or programming language
that allows you to implement complex features on
web pages — every time a web page does more than
just sit there and display static information for you to
look at — displaying timely content updates,
interactive maps, animated 2D/3D graphics, scrolling
video jukeboxes, etc. — you can bet that JavaScript is
probably involved. It is the third layer of the layer
cake of standard web technologies, two of which
(HTML and CSS) we have covered in much more
detail in other parts of the Learning Area.
 HTML is the markup language that we use to structure and
give meaning to our web content, for example defining
paragraphs, headings, and data tables, or embedding images
and videos in the page.
 CSS is a language of style rules that we use to apply styling to
our HTML content, for example setting background colors and
fonts, and laying out our content in multiple columns.
 JavaScript is a scripting language that enables you to create
dynamically updating content, control multimedia, animate
images, and pretty much everything else. (Okay, not
everything, but it is amazing what you can achieve with a few
lines of JavaScript code.)
The three layers build on top of one another nicely. Let's take
a simple text label as an example. We can mark it up using
HTML to give it structure and purpose:
<p>Player 1: Chris</p>
Then we can add some CSS into the mix to get it looking nice:
P
{ font-family: "helvetica neue", helvetica, sans-serif;
letter-spacing: 1px;
text-transform: uppercase;
text-align: center;
border: 2px solid rgb(0 0 200 / 0.6);
background: rgb(0 0 200 / 0.6);
color: rgb(255 255 255 / 1);
box-shadow: 1px 1px 2px rgb(0 0 200 / 0.4);
border-radius: 10px; padding: 3px 10px;
display: inline-block;
cursor: pointer;
}
And finally, we can add some JavaScript
to implement dynamic behavior:
const para = document.querySelector("p");
para.addEventListener("click", updateName);
function updateName()
{
const name = prompt("Enter a new name");
para.textContent = `Player 1: ${name}`;
}
The core client-side JavaScript language
consists of some common programming
features that allow you to do things like:
 Store useful values inside variables. In the above example
for instance, we ask for a new name to be entered then store
that name in a variable called name.
 Operations on pieces of text (known as "strings" in
programming). In the above example we take the string
"Player 1: " and join it to the name variable to create the
complete text label, e.g. "Player 1: Chris".
 Running code in response to certain events occurring on a
web page. We used a click event in our example above to
detect when the label is clicked and then run the code that
updates the text label.
 And much more!
JavaScript running order
 When the browser encounters a block of JavaScript, it
generally runs it in order, from top to bottom. This means
that you need to be careful what order you put things in.
For example, let's return to the block of JavaScript
const para = document.querySelector("p"); para.addEventListener("click", updateName);
function updateName()
{
const name = prompt("Enter a new name");
para.textContent = `Player 1: ${name}`;
}
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. The updateName() code block (these types of reusable code blocks are
called "functions") asks the user for a new name, and then inserts that name into the paragraph to update the display.
If you swapped the order of the first two lines of code, it would no longer work — instead, you'd get an error returned in
the browser developer console — TypeError: para is undefined. This means that the para object does not exist yet, so we can't
add an event listener to it.
Interpreted versus compiled code
 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 form before the browser runs it.
The code is received in its programmer-friendly text
form and processed directly from that.
 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 code that is then run by the
computer. The program is executed from a binary
format, which was generated from the original
program source code.
 JavaScript is a lightweight interpreted programming
language. The web browser receives the JavaScript
code in its original text form and runs the script from
that. From a technical standpoint, most modern
JavaScript interpreters actually use a technique
called just-in-time compiling to improve
performance; the JavaScript source code gets
compiled into a faster, binary format while the script is
being used, so that it can be run as quickly as
possible. However, JavaScript is still considered an
interpreted language, since the compilation is
handled at run time, rather than ahead of time
Server-side versus client-side
code
 Client-side code is code that is run on the user's computer — when a
web page is viewed, the page's client-side code is downloaded, then run
and displayed by the browser. In this module we are explicitly talking
about client-side JavaScript.
 Server-side code on the other hand is run on the
server, then its results are downloaded and
displayed in the browser. Examples of popular
server-side web languages include PHP, Python,
Ruby, ASP.NET, and even JavaScript! JavaScript
can also be used as a server-side language, for
example in the popular Node.js environment
Dynamic versus static code
 The word dynamic is used to describe both client-side JavaScript, and server-
side languages — it refers to the ability to update the display of a web page/app
to show different things in different circumstances, generating new content as
required. Server-side code dynamically generates new content on the server,
e.g. pulling data from a database, whereas client-side JavaScript dynamically
generates new content inside the browser on the client, e.g. creating a new
HTML table, filling it with data requested from the server, then displaying the
table in a web page shown to the user. The meaning is slightly different in the
two contexts, but related, and both approaches (server-side and client-side)
usually work together.
 A web page with no dynamically updating content is referred to
as static — it just shows the same content all the time.
How do you add JavaScript to your
page?
 JavaScript is applied to your HTML page in a similar
manner to CSS. Whereas CSS uses <link> elements to
apply external stylesheets and <style> elements to apply
internal stylesheets to HTML, JavaScript only needs one
friend in the world of HTML — the <script> element. Let's
learn how this works.
Internal JavaScript
 First of all, make a local copy of example
file apply-javascript.html. Save it in a directory
somewhere sensible.
 Open the file in your web browser and in your text
editor. You'll see that the HTML creates a simple
web page containing a clickable button.
 Next, go to your text editor and add the following
in your head — just before your
closing </head> tag:
<script> // JavaScript goes here </script>
Now we'll add some JavaScript inside our <script> element to make the
page do something more interesting — add the following code just below
the "// JavaScript goes here" line:
document.addEventListener("DOMContentLoaded", () => {
function createParagraph() {
const para = document.createElement("p");
para.textContent = "You clicked the button!";
document.body.appendChild(para);
}
const buttons = document.querySelectorAll("button");
for (const button of buttons) {
button.addEventListener("click", createParagraph);
}
}); Save your file and refresh the browser — now you
should see that when you click the button, a new
paragraph is generated and placed below.
External JavaScript
 First, create a new file in the same directory as
your sample HTML file. Call it script.js — make
sure it has that .js filename extension, as that's
how it is recognized as JavaScript.
 Replace your current <script> element with the
following
<script src="script.js" defer></script>
 function createParagraph() { const para =
document.createElement("p"); para.textContent =
"You clicked the button!";
document.body.appendChild(para); } const
buttons = document.querySelectorAll("button");
for (const button of buttons) {
button.addEventListener("click",
createParagraph); }
•Save and refresh your browser, and you should see the same thing! It
works just the same, but now we've got our JavaScript in an external file.
This is generally a good thing in terms of organizing your code and making
it reusable across multiple HTML files. Plus, the HTML is easier to read
without huge chunks of script dumped in it.
JavaScript Variables
 JavaScript variables are containers for storing
data values.
In this example, x, y, and z, are variables:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Variables</h2>
<p>In this example, x, y, and z are variables.</p>
<p id="demo"></p>
<script>
var x = 5;
var y = 6;
var z = x + y;
document.getElementById("demo").innerHTML =
"The value of z is: " + z;
</script>
</body>
</html>
JavaScript Numbers
JavaScript has only one type of number. Numbers can
be written with or without decimals.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Numbers</h2>
<p>Numbers can be written with or without decimals:</p>
<p id="demo"></p>
<script>
var x = 3.14;
var y = 3;
document.getElementById("demo").innerHTML = x + "<br>" + y;
</script>
</body>
</html>
JavaScript Strings
 Strings store text. Strings are written inside quotes.
You can use single or double quotes:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Strings</h2>
<p>Strings are written inside quotes. You can use single or double quotes:</p>
<p id="demo"></p>
<script>
var carName1 = "Volvo XC60"; // Double quotes
var carName2 = 'Volvo XC60'; // Single quotes
document.getElementById("demo").innerHTML =
carName1 + " " + carName2;
</script>
</body>
</html>
JavaScript Objects
 You have already learned that JavaScript variables
are containers for data values.
This code assigns a simple value (Fiat) to
a variable named car:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Variables</h2>
<p id="demo"></p>
<script>
// Create and display a variable:
var car = "Fiat";
document.getElementById("demo").innerHTML = car;
</script>
</body>
</html>
JavaScript Arrays
 JavaScript arrays are used to store multiple values in a
single variable.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arrays</h2>
<p id="demo"></p>
<script>
var cars = ["Saab", "Volvo", "BMW"];
document.getElementById("demo").innerHTML = cars;
</script>
</body>
</html>
JavaScript Functions
 A JavaScript function is a block of code designed to
perform a particular task.
A JavaScript function is executed when "something"
invokes it (calls it).
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Functions</h2>
<p>This example calls a function which performs a calculation,
and returns the result:</p>
<p id="demo"></p>
<script>
function myFunction(p1, p2) {
return p1 * p2;
}
document.getElementById("demo").innerHTML =
myFunction(4, 3);
</script>
</body>
</html>
JavaScript Where To
 JavaScript in <head>
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph
changed.";
}
</script>
</head>
<body><h2>Demo JavaScript in Head</h2>
<p id="demo">A Paragraph</p>
<button type="button" onclick="myFunction()">Try it</button>
</body>
</html>
 JavaScript in <body>
<!DOCTYPE html>
<html>
<body>
<h2>Demo JavaScript in Body</h2>
<p id="demo">A Paragraph</p>
<button type="button" onclick="myFunction()">Try it</button>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph
changed.";
}
</script>
</body>
</html>
 External JavaScript
 External file: myScript.js
function myFunction() {
document.getElementById("demo").innerHTML = "Pa
ragraph changed.";
}
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:
<script src="myScript.js"></script>
<!DOCTYPE html>
<html>
<body>
<h2>Demo External JavaScript</h2>
<p id="demo">A Paragraph.</p>
<button type="button" onclick="myFunction()">Try it</button>
<p>This example links to "myScript.js".</p>
<p>(myFunction is stored in "myScript.js")</p>
<script src="myScript.js"></script>
</body>
</html>
External JavaScript Advantages
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
To add several script files to one page - use several
script tags:
External References
An external script can be referenced in 3 different
ways:
With a full URL (a full web address)
<script src="https://www.w3schools.com/js/myScript.js"></script>
With a file path (like /js/)
<script src="/js/myScript.js"></script>
Without any path
<script src="myScript.js"></script>
What is ES6?
 ES6 stands for ECMAScript 6.
 ECMAScript was created to standardize
JavaScript, and ES6 is the 6th version of
ECMAScript, it was published in 2015, and is also
known as ECMAScript 2015.
ECMAScript, also known as JavaScript, is a programming language
adopted by the European Computer Manufacturer's Association as a
standard for performing computations in Web applications.
ECMAScript is the official client-side scripting
Why Should I Learn ES6?
React uses ES6, and you should be familiar with
some of the new features like:
 Classes
 Arrow Functions
 Variables (let, const, var)
 Array Methods like .map()
 Destructuring
 Modules
 Ternary Operator
 Spread Operator
A variable, by definition, is “a named space in the
memory” that stores values. In other words, it acts as
a container for values in a program. Variable names
are called identifiers. Following are the naming rules
for an identifier −
 Identifiers cannot be keywords.
 Identifiers can contain alphabets and numbers.
 Identifiers cannot contain spaces and special
characters, except the underscore (_) and the dollar
($) sign.
 Variable names cannot begin with a number.
Declaring variables in es6
 Before ES6 there was only one way of defining your
variables: with the var keyword. If you did not define
them, they would be assigned to the global object.
Unless you were in strict mode, then you would get
an error if your variables were undefined.
 Now, with ES6, there are three ways of defining your
variables: var, let, and const.
var x = 5.6;
 If you use var outside of a function, it belongs to
the global scope.
 If you use var inside of a function, it belongs to
that function.
 If you use var inside of a block, i.e. a for loop, the
variable is still available outside of that block.
var has a function scope, not a block scope.
let x = 5.6;
 let is the block scoped version of var, and is
limited to the block (or expression) where it is
defined.
 If you use let inside of a block, i.e. a for loop, the
variable is only available inside of that loop.
let has a block scope.
const x = 5.6;
 const is a variable that once it has been created,
its value can never change.
const has a block scope.
The keyword const is a bit misleading.
It does not define a constant value. It defines a constant reference to a value.
Because of this you can NOT:
Reassign a constant value
Reassign a constant array
Reassign a constant object
But you CAN:
Change the elements of constant array
Change the properties of constant object
JavaScript Arrow Function
 Arrow functions were introduced in ES6.
 It allows you to create functions in a
cleaner way compared to regular
functions.
 Arrow functions allow us to write shorter function
syntax:
let myFunction = (a, b) => a * b;
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Functions</h1>
<h2>The Arrow Function</h2>
<p>This example shows the syntax of an Arrow Function, and
how to use it.</p>
<p id="demo"></p>
<script>
let myFunction = (a, b) => a * b;
document.getElementById("demo").innerHTML = myFunction(4,
5);
</script>
</body>
Without Arrow function :
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Functions</h1>
<p>This example shows the syntax of a function, without the use of arrow function syntax.</p>
<p id="demo"></p>
<script>
let hello = "";
hello = function() {
return "Hello World!";
}
document.getElementById("demo").innerHTML = hello();
</script>
</body>
</html>
With Arrow Function:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Functions</h1>
<h2>The Arrow Function</h2>
<p>This example shows the syntax of an Arrow Function, and how to use it.</p>
<p id="demo"></p>
<script>
let hello = "";
hello = () => {
return "Hello World!";
}
document.getElementById("demo").innerHTML = hello();
</script>
</body>
</html>
It gets shorter! If the function has only one statement, and the
statement returns a value, you can remove the
brackets and the return keyword:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Functions</h1>
<h2>The Arrow Function</h2>
<p>This example shows an Arrow Function without the brackets or the return keyword.</p>
<p id="demo"></p>
<script>
let hello = "";
hello = () => "Hello World!";
document.getElementById("demo").innerHTML = hello();
</script>
</body>
</html>

More Related Content

Similar to Lecture-15.pptx

JAVA SCRIPT
JAVA SCRIPTJAVA SCRIPT
JAVA SCRIPT
Go4Guru
 
JavaScript
JavaScriptJavaScript
JavaScript
Gulbir Chaudhary
 
Introduction to Shiny for building web apps in R
Introduction to Shiny for building web apps in RIntroduction to Shiny for building web apps in R
Introduction to Shiny for building web apps in R
Paul Richards
 
Javascript tutorial
Javascript tutorialJavascript tutorial
Javascript tutorial
Avinash Malhotra
 
Overview of PHP and MYSQL
Overview of PHP and MYSQLOverview of PHP and MYSQL
Overview of PHP and MYSQL
Deblina Chowdhury
 
Javascript
JavascriptJavascript
Javascript
mussawir20
 
Basic Java script handouts for students
Basic Java script handouts for students Basic Java script handouts for students
Basic Java script handouts for students
shafiq sangi
 
8.-Javascript-report powerpoint presentation
8.-Javascript-report powerpoint presentation8.-Javascript-report powerpoint presentation
8.-Javascript-report powerpoint presentation
JohnLagman3
 
JS BASICS JAVA SCRIPT SCRIPTING
JS BASICS JAVA SCRIPT SCRIPTINGJS BASICS JAVA SCRIPT SCRIPTING
JS BASICS JAVA SCRIPT SCRIPTING
Arulkumar
 
Java script by Act Academy
Java script by Act AcademyJava script by Act Academy
Java script by Act Academy
actanimation
 
Java script
Java scriptJava script
Java script
Fajar Baskoro
 
Active server pages
Active server pagesActive server pages
Active server pages
mcatahir947
 
Java script Learn Easy
Java script Learn Easy Java script Learn Easy
Java script Learn Easy
prince Loffar
 
Introduction to java script
Introduction to java scriptIntroduction to java script
Introduction to java script
nanjil1984
 
Presentation
PresentationPresentation
Presentation
Chetan Kataria
 

Similar to Lecture-15.pptx (20)

JAVA SCRIPT
JAVA SCRIPTJAVA SCRIPT
JAVA SCRIPT
 
JavaScript
JavaScriptJavaScript
JavaScript
 
Introduction to Shiny for building web apps in R
Introduction to Shiny for building web apps in RIntroduction to Shiny for building web apps in R
Introduction to Shiny for building web apps in R
 
Lect35 javascript
Lect35 javascriptLect35 javascript
Lect35 javascript
 
Javascript tutorial
Javascript tutorialJavascript tutorial
Javascript tutorial
 
Overview of PHP and MYSQL
Overview of PHP and MYSQLOverview of PHP and MYSQL
Overview of PHP and MYSQL
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
Javascript
JavascriptJavascript
Javascript
 
Ajax.ppt
Ajax.pptAjax.ppt
Ajax.ppt
 
PPT
PPTPPT
PPT
 
Basic Java script handouts for students
Basic Java script handouts for students Basic Java script handouts for students
Basic Java script handouts for students
 
8.-Javascript-report powerpoint presentation
8.-Javascript-report powerpoint presentation8.-Javascript-report powerpoint presentation
8.-Javascript-report powerpoint presentation
 
JS BASICS JAVA SCRIPT SCRIPTING
JS BASICS JAVA SCRIPT SCRIPTINGJS BASICS JAVA SCRIPT SCRIPTING
JS BASICS JAVA SCRIPT SCRIPTING
 
Java script by Act Academy
Java script by Act AcademyJava script by Act Academy
Java script by Act Academy
 
Java script
Java scriptJava script
Java script
 
Active server pages
Active server pagesActive server pages
Active server pages
 
Ajax
AjaxAjax
Ajax
 
Java script Learn Easy
Java script Learn Easy Java script Learn Easy
Java script Learn Easy
 
Introduction to java script
Introduction to java scriptIntroduction to java script
Introduction to java script
 
Presentation
PresentationPresentation
Presentation
 

More from vishal choudhary

SE-Lecture1.ppt
SE-Lecture1.pptSE-Lecture1.ppt
SE-Lecture1.ppt
vishal choudhary
 
SE-Testing.ppt
SE-Testing.pptSE-Testing.ppt
SE-Testing.ppt
vishal choudhary
 
SE-CyclomaticComplexityand Testing.ppt
SE-CyclomaticComplexityand Testing.pptSE-CyclomaticComplexityand Testing.ppt
SE-CyclomaticComplexityand Testing.ppt
vishal choudhary
 
SE-Lecture-7.pptx
SE-Lecture-7.pptxSE-Lecture-7.pptx
SE-Lecture-7.pptx
vishal choudhary
 
Se-Lecture-6.ppt
Se-Lecture-6.pptSe-Lecture-6.ppt
Se-Lecture-6.ppt
vishal choudhary
 
SE-Lecture-5.pptx
SE-Lecture-5.pptxSE-Lecture-5.pptx
SE-Lecture-5.pptx
vishal choudhary
 
SE-Lecture-8.pptx
SE-Lecture-8.pptxSE-Lecture-8.pptx
SE-Lecture-8.pptx
vishal choudhary
 
SE-coupling and cohesion.ppt
SE-coupling and cohesion.pptSE-coupling and cohesion.ppt
SE-coupling and cohesion.ppt
vishal choudhary
 
SE-Lecture-2.pptx
SE-Lecture-2.pptxSE-Lecture-2.pptx
SE-Lecture-2.pptx
vishal choudhary
 
SE-software design.ppt
SE-software design.pptSE-software design.ppt
SE-software design.ppt
vishal choudhary
 
SE1.ppt
SE1.pptSE1.ppt
SE-Lecture-4.pptx
SE-Lecture-4.pptxSE-Lecture-4.pptx
SE-Lecture-4.pptx
vishal choudhary
 
SE-Lecture=3.pptx
SE-Lecture=3.pptxSE-Lecture=3.pptx
SE-Lecture=3.pptx
vishal choudhary
 
Multimedia-Lecture-Animation.pptx
Multimedia-Lecture-Animation.pptxMultimedia-Lecture-Animation.pptx
Multimedia-Lecture-Animation.pptx
vishal choudhary
 
MultimediaLecture5.pptx
MultimediaLecture5.pptxMultimediaLecture5.pptx
MultimediaLecture5.pptx
vishal choudhary
 
Multimedia-Lecture-7.pptx
Multimedia-Lecture-7.pptxMultimedia-Lecture-7.pptx
Multimedia-Lecture-7.pptx
vishal choudhary
 
MultiMedia-Lecture-4.pptx
MultiMedia-Lecture-4.pptxMultiMedia-Lecture-4.pptx
MultiMedia-Lecture-4.pptx
vishal choudhary
 
Multimedia-Lecture-6.pptx
Multimedia-Lecture-6.pptxMultimedia-Lecture-6.pptx
Multimedia-Lecture-6.pptx
vishal choudhary
 
Multimedia-Lecture-3.pptx
Multimedia-Lecture-3.pptxMultimedia-Lecture-3.pptx
Multimedia-Lecture-3.pptx
vishal choudhary
 

More from vishal choudhary (20)

SE-Lecture1.ppt
SE-Lecture1.pptSE-Lecture1.ppt
SE-Lecture1.ppt
 
SE-Testing.ppt
SE-Testing.pptSE-Testing.ppt
SE-Testing.ppt
 
SE-CyclomaticComplexityand Testing.ppt
SE-CyclomaticComplexityand Testing.pptSE-CyclomaticComplexityand Testing.ppt
SE-CyclomaticComplexityand Testing.ppt
 
SE-Lecture-7.pptx
SE-Lecture-7.pptxSE-Lecture-7.pptx
SE-Lecture-7.pptx
 
Se-Lecture-6.ppt
Se-Lecture-6.pptSe-Lecture-6.ppt
Se-Lecture-6.ppt
 
SE-Lecture-5.pptx
SE-Lecture-5.pptxSE-Lecture-5.pptx
SE-Lecture-5.pptx
 
XML.pptx
XML.pptxXML.pptx
XML.pptx
 
SE-Lecture-8.pptx
SE-Lecture-8.pptxSE-Lecture-8.pptx
SE-Lecture-8.pptx
 
SE-coupling and cohesion.ppt
SE-coupling and cohesion.pptSE-coupling and cohesion.ppt
SE-coupling and cohesion.ppt
 
SE-Lecture-2.pptx
SE-Lecture-2.pptxSE-Lecture-2.pptx
SE-Lecture-2.pptx
 
SE-software design.ppt
SE-software design.pptSE-software design.ppt
SE-software design.ppt
 
SE1.ppt
SE1.pptSE1.ppt
SE1.ppt
 
SE-Lecture-4.pptx
SE-Lecture-4.pptxSE-Lecture-4.pptx
SE-Lecture-4.pptx
 
SE-Lecture=3.pptx
SE-Lecture=3.pptxSE-Lecture=3.pptx
SE-Lecture=3.pptx
 
Multimedia-Lecture-Animation.pptx
Multimedia-Lecture-Animation.pptxMultimedia-Lecture-Animation.pptx
Multimedia-Lecture-Animation.pptx
 
MultimediaLecture5.pptx
MultimediaLecture5.pptxMultimediaLecture5.pptx
MultimediaLecture5.pptx
 
Multimedia-Lecture-7.pptx
Multimedia-Lecture-7.pptxMultimedia-Lecture-7.pptx
Multimedia-Lecture-7.pptx
 
MultiMedia-Lecture-4.pptx
MultiMedia-Lecture-4.pptxMultiMedia-Lecture-4.pptx
MultiMedia-Lecture-4.pptx
 
Multimedia-Lecture-6.pptx
Multimedia-Lecture-6.pptxMultimedia-Lecture-6.pptx
Multimedia-Lecture-6.pptx
 
Multimedia-Lecture-3.pptx
Multimedia-Lecture-3.pptxMultimedia-Lecture-3.pptx
Multimedia-Lecture-3.pptx
 

Recently uploaded

World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Ashish Kohli
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 

Recently uploaded (20)

World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 

Lecture-15.pptx

  • 2. JavaScript, often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of websites use JavaScript on the client side for webpage behaviour, often incorporating third-party libraries.
  • 3. A high-level definition  JavaScript is a scripting or programming language that allows you to implement complex features on web pages — every time a web page does more than just sit there and display static information for you to look at — displaying timely content updates, interactive maps, animated 2D/3D graphics, scrolling video jukeboxes, etc. — you can bet that JavaScript is probably involved. It is the third layer of the layer cake of standard web technologies, two of which (HTML and CSS) we have covered in much more detail in other parts of the Learning Area.
  • 4.  HTML is the markup language that we use to structure and give meaning to our web content, for example defining paragraphs, headings, and data tables, or embedding images and videos in the page.  CSS is a language of style rules that we use to apply styling to our HTML content, for example setting background colors and fonts, and laying out our content in multiple columns.  JavaScript is a scripting language that enables you to create dynamically updating content, control multimedia, animate images, and pretty much everything else. (Okay, not everything, but it is amazing what you can achieve with a few lines of JavaScript code.)
  • 5. The three layers build on top of one another nicely. Let's take a simple text label as an example. We can mark it up using HTML to give it structure and purpose: <p>Player 1: Chris</p> Then we can add some CSS into the mix to get it looking nice: P { font-family: "helvetica neue", helvetica, sans-serif; letter-spacing: 1px; text-transform: uppercase; text-align: center; border: 2px solid rgb(0 0 200 / 0.6); background: rgb(0 0 200 / 0.6); color: rgb(255 255 255 / 1); box-shadow: 1px 1px 2px rgb(0 0 200 / 0.4); border-radius: 10px; padding: 3px 10px; display: inline-block; cursor: pointer; }
  • 6. And finally, we can add some JavaScript to implement dynamic behavior: const para = document.querySelector("p"); para.addEventListener("click", updateName); function updateName() { const name = prompt("Enter a new name"); para.textContent = `Player 1: ${name}`; }
  • 7. The core client-side JavaScript language consists of some common programming features that allow you to do things like:  Store useful values inside variables. In the above example for instance, we ask for a new name to be entered then store that name in a variable called name.  Operations on pieces of text (known as "strings" in programming). In the above example we take the string "Player 1: " and join it to the name variable to create the complete text label, e.g. "Player 1: Chris".  Running code in response to certain events occurring on a web page. We used a click event in our example above to detect when the label is clicked and then run the code that updates the text label.  And much more!
  • 8. JavaScript running order  When the browser encounters a block of JavaScript, it generally runs it in order, from top to bottom. This means that you need to be careful what order you put things in. For example, let's return to the block of JavaScript const para = document.querySelector("p"); para.addEventListener("click", updateName); function updateName() { const name = prompt("Enter a new name"); para.textContent = `Player 1: ${name}`; } 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. The updateName() code block (these types of reusable code blocks are called "functions") asks the user for a new name, and then inserts that name into the paragraph to update the display. If you swapped the order of the first two lines of code, it would no longer work — instead, you'd get an error returned in the browser developer console — TypeError: para is undefined. This means that the para object does not exist yet, so we can't add an event listener to it.
  • 9. Interpreted versus compiled code  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 form before the browser runs it. The code is received in its programmer-friendly text form and processed directly from that.  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 code that is then run by the computer. The program is executed from a binary format, which was generated from the original program source code.
  • 10.  JavaScript is a lightweight interpreted programming language. The web browser receives the JavaScript code in its original text form and runs the script from that. From a technical standpoint, most modern JavaScript interpreters actually use a technique called just-in-time compiling to improve performance; the JavaScript source code gets compiled into a faster, binary format while the script is being used, so that it can be run as quickly as possible. However, JavaScript is still considered an interpreted language, since the compilation is handled at run time, rather than ahead of time
  • 11. Server-side versus client-side code  Client-side code is code that is run on the user's computer — when a web page is viewed, the page's client-side code is downloaded, then run and displayed by the browser. In this module we are explicitly talking about client-side JavaScript.  Server-side code on the other hand is run on the server, then its results are downloaded and displayed in the browser. Examples of popular server-side web languages include PHP, Python, Ruby, ASP.NET, and even JavaScript! JavaScript can also be used as a server-side language, for example in the popular Node.js environment
  • 12. Dynamic versus static code  The word dynamic is used to describe both client-side JavaScript, and server- side languages — it refers to the ability to update the display of a web page/app to show different things in different circumstances, generating new content as required. Server-side code dynamically generates new content on the server, e.g. pulling data from a database, whereas client-side JavaScript dynamically generates new content inside the browser on the client, e.g. creating a new HTML table, filling it with data requested from the server, then displaying the table in a web page shown to the user. The meaning is slightly different in the two contexts, but related, and both approaches (server-side and client-side) usually work together.  A web page with no dynamically updating content is referred to as static — it just shows the same content all the time.
  • 13. How do you add JavaScript to your page?  JavaScript is applied to your HTML page in a similar manner to CSS. Whereas CSS uses <link> elements to apply external stylesheets and <style> elements to apply internal stylesheets to HTML, JavaScript only needs one friend in the world of HTML — the <script> element. Let's learn how this works.
  • 14. Internal JavaScript  First of all, make a local copy of example file apply-javascript.html. Save it in a directory somewhere sensible.  Open the file in your web browser and in your text editor. You'll see that the HTML creates a simple web page containing a clickable button.  Next, go to your text editor and add the following in your head — just before your closing </head> tag: <script> // JavaScript goes here </script>
  • 15. Now we'll add some JavaScript inside our <script> element to make the page do something more interesting — add the following code just below the "// JavaScript goes here" line: document.addEventListener("DOMContentLoaded", () => { function createParagraph() { const para = document.createElement("p"); para.textContent = "You clicked the button!"; document.body.appendChild(para); } const buttons = document.querySelectorAll("button"); for (const button of buttons) { button.addEventListener("click", createParagraph); } }); Save your file and refresh the browser — now you should see that when you click the button, a new paragraph is generated and placed below.
  • 16. External JavaScript  First, create a new file in the same directory as your sample HTML file. Call it script.js — make sure it has that .js filename extension, as that's how it is recognized as JavaScript.  Replace your current <script> element with the following <script src="script.js" defer></script>
  • 17.  function createParagraph() { const para = document.createElement("p"); para.textContent = "You clicked the button!"; document.body.appendChild(para); } const buttons = document.querySelectorAll("button"); for (const button of buttons) { button.addEventListener("click", createParagraph); } •Save and refresh your browser, and you should see the same thing! It works just the same, but now we've got our JavaScript in an external file. This is generally a good thing in terms of organizing your code and making it reusable across multiple HTML files. Plus, the HTML is easier to read without huge chunks of script dumped in it.
  • 18. JavaScript Variables  JavaScript variables are containers for storing data values. In this example, x, y, and z, are variables: <!DOCTYPE html> <html> <body> <h2>JavaScript Variables</h2> <p>In this example, x, y, and z are variables.</p> <p id="demo"></p> <script> var x = 5; var y = 6; var z = x + y; document.getElementById("demo").innerHTML = "The value of z is: " + z; </script> </body> </html>
  • 19. JavaScript Numbers JavaScript has only one type of number. Numbers can be written with or without decimals. <!DOCTYPE html> <html> <body> <h2>JavaScript Numbers</h2> <p>Numbers can be written with or without decimals:</p> <p id="demo"></p> <script> var x = 3.14; var y = 3; document.getElementById("demo").innerHTML = x + "<br>" + y; </script> </body> </html>
  • 20. JavaScript Strings  Strings store text. Strings are written inside quotes. You can use single or double quotes: <!DOCTYPE html> <html> <body> <h2>JavaScript Strings</h2> <p>Strings are written inside quotes. You can use single or double quotes:</p> <p id="demo"></p> <script> var carName1 = "Volvo XC60"; // Double quotes var carName2 = 'Volvo XC60'; // Single quotes document.getElementById("demo").innerHTML = carName1 + " " + carName2; </script> </body> </html>
  • 21. JavaScript Objects  You have already learned that JavaScript variables are containers for data values. This code assigns a simple value (Fiat) to a variable named car: <!DOCTYPE html> <html> <body> <h2>JavaScript Variables</h2> <p id="demo"></p> <script> // Create and display a variable: var car = "Fiat"; document.getElementById("demo").innerHTML = car; </script> </body> </html>
  • 22. JavaScript Arrays  JavaScript arrays are used to store multiple values in a single variable. <!DOCTYPE html> <html> <body> <h2>JavaScript Arrays</h2> <p id="demo"></p> <script> var cars = ["Saab", "Volvo", "BMW"]; document.getElementById("demo").innerHTML = cars; </script> </body> </html>
  • 23. JavaScript Functions  A JavaScript function is a block of code designed to perform a particular task. A JavaScript function is executed when "something" invokes it (calls it). <!DOCTYPE html> <html> <body> <h2>JavaScript Functions</h2> <p>This example calls a function which performs a calculation, and returns the result:</p> <p id="demo"></p> <script> function myFunction(p1, p2) { return p1 * p2; } document.getElementById("demo").innerHTML = myFunction(4, 3); </script> </body> </html>
  • 24. JavaScript Where To  JavaScript in <head> <!DOCTYPE html> <html> <head> <script> function myFunction() { document.getElementById("demo").innerHTML = "Paragraph changed."; } </script> </head> <body><h2>Demo JavaScript in Head</h2> <p id="demo">A Paragraph</p> <button type="button" onclick="myFunction()">Try it</button> </body> </html>
  • 25.  JavaScript in <body> <!DOCTYPE html> <html> <body> <h2>Demo JavaScript in Body</h2> <p id="demo">A Paragraph</p> <button type="button" onclick="myFunction()">Try it</button> <script> function myFunction() { document.getElementById("demo").innerHTML = "Paragraph changed."; } </script> </body> </html>
  • 26.  External JavaScript  External file: myScript.js function myFunction() { document.getElementById("demo").innerHTML = "Pa ragraph changed."; } 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: <script src="myScript.js"></script>
  • 27. <!DOCTYPE html> <html> <body> <h2>Demo External JavaScript</h2> <p id="demo">A Paragraph.</p> <button type="button" onclick="myFunction()">Try it</button> <p>This example links to "myScript.js".</p> <p>(myFunction is stored in "myScript.js")</p> <script src="myScript.js"></script> </body> </html>
  • 28. External JavaScript Advantages 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 To add several script files to one page - use several script tags:
  • 29. External References An external script can be referenced in 3 different ways: With a full URL (a full web address) <script src="https://www.w3schools.com/js/myScript.js"></script> With a file path (like /js/) <script src="/js/myScript.js"></script> Without any path <script src="myScript.js"></script>
  • 30. What is ES6?  ES6 stands for ECMAScript 6.  ECMAScript was created to standardize JavaScript, and ES6 is the 6th version of ECMAScript, it was published in 2015, and is also known as ECMAScript 2015. ECMAScript, also known as JavaScript, is a programming language adopted by the European Computer Manufacturer's Association as a standard for performing computations in Web applications. ECMAScript is the official client-side scripting
  • 31. Why Should I Learn ES6? React uses ES6, and you should be familiar with some of the new features like:  Classes  Arrow Functions  Variables (let, const, var)  Array Methods like .map()  Destructuring  Modules  Ternary Operator  Spread Operator
  • 32. A variable, by definition, is “a named space in the memory” that stores values. In other words, it acts as a container for values in a program. Variable names are called identifiers. Following are the naming rules for an identifier −  Identifiers cannot be keywords.  Identifiers can contain alphabets and numbers.  Identifiers cannot contain spaces and special characters, except the underscore (_) and the dollar ($) sign.  Variable names cannot begin with a number.
  • 33. Declaring variables in es6  Before ES6 there was only one way of defining your variables: with the var keyword. If you did not define them, they would be assigned to the global object. Unless you were in strict mode, then you would get an error if your variables were undefined.  Now, with ES6, there are three ways of defining your variables: var, let, and const.
  • 34. var x = 5.6;  If you use var outside of a function, it belongs to the global scope.  If you use var inside of a function, it belongs to that function.  If you use var inside of a block, i.e. a for loop, the variable is still available outside of that block. var has a function scope, not a block scope.
  • 35. let x = 5.6;  let is the block scoped version of var, and is limited to the block (or expression) where it is defined.  If you use let inside of a block, i.e. a for loop, the variable is only available inside of that loop. let has a block scope.
  • 36. const x = 5.6;  const is a variable that once it has been created, its value can never change. const has a block scope. The keyword const is a bit misleading. It does not define a constant value. It defines a constant reference to a value. Because of this you can NOT: Reassign a constant value Reassign a constant array Reassign a constant object But you CAN: Change the elements of constant array Change the properties of constant object
  • 37. JavaScript Arrow Function  Arrow functions were introduced in ES6.  It allows you to create functions in a cleaner way compared to regular functions.  Arrow functions allow us to write shorter function syntax: let myFunction = (a, b) => a * b; <!DOCTYPE html> <html> <body> <h1>JavaScript Functions</h1> <h2>The Arrow Function</h2> <p>This example shows the syntax of an Arrow Function, and how to use it.</p> <p id="demo"></p> <script> let myFunction = (a, b) => a * b; document.getElementById("demo").innerHTML = myFunction(4, 5); </script> </body>
  • 38. Without Arrow function : <!DOCTYPE html> <html> <body> <h1>JavaScript Functions</h1> <p>This example shows the syntax of a function, without the use of arrow function syntax.</p> <p id="demo"></p> <script> let hello = ""; hello = function() { return "Hello World!"; } document.getElementById("demo").innerHTML = hello(); </script> </body> </html>
  • 39. With Arrow Function: <!DOCTYPE html> <html> <body> <h1>JavaScript Functions</h1> <h2>The Arrow Function</h2> <p>This example shows the syntax of an Arrow Function, and how to use it.</p> <p id="demo"></p> <script> let hello = ""; hello = () => { return "Hello World!"; } document.getElementById("demo").innerHTML = hello(); </script> </body> </html>
  • 40. It gets shorter! If the function has only one statement, and the statement returns a value, you can remove the brackets and the return keyword: <!DOCTYPE html> <html> <body> <h1>JavaScript Functions</h1> <h2>The Arrow Function</h2> <p>This example shows an Arrow Function without the brackets or the return keyword.</p> <p id="demo"></p> <script> let hello = ""; hello = () => "Hello World!"; document.getElementById("demo").innerHTML = hello(); </script> </body> </html>