SlideShare a Scribd company logo
INTERNET-BASED PROGRAMMING
Mr. Stephen Njuguna
JAVASCRIPT
DEFINATION TERMS
 JavaScript is the world's most popular programming
language.
 JavaScript is the programming language of the Web.
 JavaScript is easy to learn.
 JavaScript from basic to advanced
 JavaScript is one of the 3 languages all web developers must learn:
i. HTML to define the content of web pages
ii. CSS to specify the layout of web pages
iii. JavaScript to program the behavior of web pages
My First JavaScript
<!DOCTYPE html>
<html>
<body>
<h2>My First JavaScript</h2>
<button type="button"
onclick="document.getElementById('demo').innerHTML =
Date()">Click me to display Date and Time.</button>
<p id="demo"></p>
</body></html>
NB: The example Above "finds" an HTML element (with id="demo"), and changes
the element content (innerHTML) to "Hello JavaScript"
What is JavaScript?
JavaScript is the programming language of the web.
It can update and change both HTML and CSS.
It can calculate, manipulate and validate data.
JavaScript Can Change HTML Content
One of many JavaScript HTML methods is
getElementById().
Old JavaScript examples may use a type attribute:
<script type="text/javascript">.
The type attribute is not required. JavaScript is the
default scripting language in HTML.
The <script> Tag
• In HTML, JavaScript code is inserted between <script> and </script>
tags.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript in Body</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "My First JavaScript";
</script>
</body>
</html>
NOTE:
A JavaScript function is a block of JavaScript code, that can be executed
when "called" for.
For example,
a function can be called when an event occurs, like when the user clicks a
button.
You can place any number of scripts in an HTML document.
Scripts can be placed in the <body>, or in the <head> section of an
HTML page, or in both.
In following example (next slide), a JavaScript function is placed in the
<head> section of an HTML page.
The function is invoked (called) when a button is clicked:
My First JavaScript
<!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 Output
 JavaScript can "display" data in different ways:
i. Writing into an HTML element, using innerHTML or innerText.
ii. Writing into the HTML output using document.write().
iii. Writing into an alert box, using window.alert().
iv. Writing into the browser console, using console.log().
Using innerHTML
 To access an HTML element, you can use the document.
getElementById (id) method.
 Use the id attribute to identify the HTML element.
 Then use the innerHTML property to change the HTML content of
the HTML element:
 Changing the innerHTML property of an HTML element is the
most common way to display data in HTML
Using innerHTML- EXAMPLE
<!DOCTYPE html>
<html><body>
<h1>My Web Page</h1>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "<h2>Hello
World</h2>";
</script>
</body></html>
Using innerText
 To access an HTML element, use the document. getElementById
(id) method.
 Then use the innerText property to change the inner text of the
HTML element:
 The document.write() method should only be used for testing.
Using innerHTML- EXAMPLE
<!DOCTYPE html>
<html>
<body>
<h2>My First Web Page</h2>
<p>My first paragraph.</p>
<button type="button" onclick="document.write(5 + 6)">Try
it</button>
</body>
</html>
Using window.alert()
 You can use an alert box to display data.
 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:
Using window.alert()- EXAMPLE
<!DOCTYPE html>
<html><body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<script>
window.alert(5 + 6);
</script>
</body></html>
<!DOCTYPE html>
<html><body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<script>
alert(5 + 6);
</script>
</body></html>
EXAMPLE 1 EXAMPLE 2
Using console.log()
For debugging purposes, you can call the console.log() method in the
browser to display data.
Using console.log()- EXAMPLE
<!DOCTYPE html>
<html><body>
<h2>Activate Debugging</h2>
<p>F12 on your keyboard will activate debugging.</p>
<p>Then select "Console" in the debugger menu.</p><p>Then
click Run again.</p>
<script>
console.log(5 + 6);
</script>
</body></html>
JavaScript Print
 JavaScript does not have any print object or print methods.
 You cannot access output devices from JavaScript.
 The only exception is that you can call the window.print()
method in the browser to print the content of the current window.
JavaScript Print- EXAMPLE
<!DOCTYPE html>
<html>
<body>
<h2>The window.print() Method</h2>
<p>Click the button to print the current page.</p>
<button onclick="window.print()">Print this page</button>
</body>
</html>
JavaScript Statements
 A computer program is a list of "instructions" to be "executed"
by a computer.
 In a programming language, these programming instructions are
called statements.
 A JavaScript program is a list of programming statements.
 In HTML, JavaScript programs are executed by the web browser.
 JavaScript statements are composed of:
Values, Operators, Expressions, Keywords, and Comments.
Eg. document.getElementById("demo").innerHTML = "Hello Dolly.";
 This statement tells the browser to write "Hello Dolly." inside an
HTML element with id="demo":
JavaScript Statements
 Most JavaScript programs contain many JavaScript statements.
 The statements are executed, one by one, in the same order as they are
written.
 JavaScript programs (and JavaScript statements) are often called
JavaScript code.
Semicolons ;
 Semicolons separate JavaScript statements.
 Add a semicolon at the end of each executable statement: eg.
let a, b, c; // Declare 3 variables
a = 5; // Assign the value 5 to a
b = 6; // Assign the value 6 to b
c = a + b; // Assign the sum of a and b to c
 When separated by semicolons, multiple statements on one line are allowed:
a = 5; b = 6; c = a + b;
Semicolons ;
 On the web, you might see examples without semicolons.
 Ending statements with semicolon is not required, but highly recommended.
JavaScript White Space
 JavaScript ignores multiple spaces. You can add white space to your script
to make it more readable.
 The following lines are equivalent:
let person = "Hege";
let person="Hege";
 A good practice is to put spaces around operators ( = + - * / ): e.g.
let x = y + z;
JavaScript Statements- EXAMPLE 1
<!DOCTYPE html>
<html><body><h2>JavaScript Statements</h2>
<p>A <b>JavaScript program</b> is a list of <b>statements</b> to be
executed by a computer.</p>
<p id="demo"></p><script>
let x, y, z; // Statement 1
x = 5; // Statement 2
y = 6; // Statement 3
z = x + y; // Statement 4
document.getElementById("demo").innerHTML =
"The value of z is " + z + ".";
</script></body></html>
JavaScript Statements- EXAMPLE 2
<!DOCTYPE html>
<html><body>
<h2>JavaScript Statements</h2>
<p>In HTML, JavaScript statements are executed by the browser.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello Dolly.";
</script>
</body>
</html>
JavaScript Line Length and Line Breaks
 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:.
JavaScript Line Length and Line Breaks- EXAMPLE
<!DOCTYPE html>
<html><body>
<h2>JavaScript Statements</h2>
<p>
The best place to break a code line is after an operator or a
comma.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"Hello Dolly!";
</script>
</body></html>
JavaScript Code Blocks
 JavaScript statements can be grouped together in code blocks, inside curly
brackets {...}.
 The purpose of code blocks is to define statements to be executed together.
 One place you will find statements grouped together in blocks, is in
JavaScript functions:
function myFunction()
{
document.getElementById("demo1").innerHTML = "Hello Dolly!";
document.getElementById("demo2").innerHTML = "How are you?";
}
JavaScript Code Blocks- EXAMPLE
<!DOCTYPE html>
<html><body><h2>JavaScript Statements</h2><p>JavaScript code
blocks are written between { and }</p>
<button type="button" onclick="myFunction()"> Click Me!
</button>
<p id="demo1"></p><p id="demo2"></p><script>
function myFunction() {
document.getElementById("demo1").innerHTML = "Hello
Dolly!";
document.getElementById("demo2").innerHTML = "How are
you?";
} </script></body></html>
JavaScript Keywords
 JavaScript statements often start with a keyword to identify the JavaScript
action to be performed.
 JavaScript keywords are reserved words. Reserved words cannot be
used as names for variables.
JavaScript Keywords
KEYWORD DESCRIPTION
var Declares a variable
let Declares a block variable
const Declares a block constant
if Marks a block of statements to be executed on a condition
switch Marks a block of statements to be executed in different cases
for Marks a block of statements to be executed in a loop
function Declares a function
return Exits a function
try Implements error handling to a block of statements
JavaScript Keywords
 JavaScript statements often start with a keyword to identify the JavaScript
action to be performed.
 JavaScript keywords are reserved words. Reserved words cannot be
used as names for variables.

More Related Content

Similar to Internet Based Programming -3-JAVASCRIPT (20)

PPTX
JAVASCRIPT 1.pptx.pptx
BeingPrime
 
PPTX
lec 14-15 Jquery_All About J-query_.pptx
MuhammadAbubakar114879
 
PPTX
JavaScript Operators
Dr. Jasmine Beulah Gnanadurai
 
RTF
Java script frame window
H K
 
PDF
Javascript
orestJump
 
PPTX
jQuery
PumoTechnovation
 
PPTX
Java Script
Kalidass Balasubramaniam
 
PPS
Java script
Mohammed Sheikh Salem
 
PPT
eXo SEA - JavaScript Introduction Training
Hoat Le
 
PPTX
wp-UNIT_III.pptx
GANDHAMKUMAR2
 
PDF
Essential_Javascript_--_A_Javascript_Tutorial
tutorialsruby
 
PDF
Essential_Javascript_--_A_Javascript_Tutorial
tutorialsruby
 
PDF
Essential Javascript -- A Javascript &lt;b>Tutorial&lt;/b>
tutorialsruby
 
PDF
&lt;img src="../i/r_14.png" />
tutorialsruby
 
PPTX
Javascript note for engineering notes.pptx
engineeradda55
 
PPT
Java script
Fajar Baskoro
 
PDF
Basic JavaScript Tutorial
DHTMLExtreme
 
PPTX
Introduction to JavaScript Basics.
Hassan Ahmed Baig - Web Developer
 
DOC
14922 java script built (1)
dineshrana201992
 
PPTX
Introduction to Web Technologies PPT.pptx
Kunal Kalamkar
 
JAVASCRIPT 1.pptx.pptx
BeingPrime
 
lec 14-15 Jquery_All About J-query_.pptx
MuhammadAbubakar114879
 
JavaScript Operators
Dr. Jasmine Beulah Gnanadurai
 
Java script frame window
H K
 
Javascript
orestJump
 
eXo SEA - JavaScript Introduction Training
Hoat Le
 
wp-UNIT_III.pptx
GANDHAMKUMAR2
 
Essential_Javascript_--_A_Javascript_Tutorial
tutorialsruby
 
Essential_Javascript_--_A_Javascript_Tutorial
tutorialsruby
 
Essential Javascript -- A Javascript &lt;b>Tutorial&lt;/b>
tutorialsruby
 
&lt;img src="../i/r_14.png" />
tutorialsruby
 
Javascript note for engineering notes.pptx
engineeradda55
 
Java script
Fajar Baskoro
 
Basic JavaScript Tutorial
DHTMLExtreme
 
Introduction to JavaScript Basics.
Hassan Ahmed Baig - Web Developer
 
14922 java script built (1)
dineshrana201992
 
Introduction to Web Technologies PPT.pptx
Kunal Kalamkar
 

Recently uploaded (20)

PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Biography of Daniel Podor.pdf
Daniel Podor
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
Ad

Internet Based Programming -3-JAVASCRIPT

  • 2. DEFINATION TERMS  JavaScript is the world's most popular programming language.  JavaScript is the programming language of the Web.  JavaScript is easy to learn.  JavaScript from basic to advanced  JavaScript is one of the 3 languages all web developers must learn: i. HTML to define the content of web pages ii. CSS to specify the layout of web pages iii. JavaScript to program the behavior of web pages
  • 3. My First JavaScript <!DOCTYPE html> <html> <body> <h2>My First JavaScript</h2> <button type="button" onclick="document.getElementById('demo').innerHTML = Date()">Click me to display Date and Time.</button> <p id="demo"></p> </body></html> NB: The example Above "finds" an HTML element (with id="demo"), and changes the element content (innerHTML) to "Hello JavaScript"
  • 4. What is JavaScript? JavaScript is the programming language of the web. It can update and change both HTML and CSS. It can calculate, manipulate and validate data. JavaScript Can Change HTML Content One of many JavaScript HTML methods is getElementById(). Old JavaScript examples may use a type attribute: <script type="text/javascript">. The type attribute is not required. JavaScript is the default scripting language in HTML.
  • 5. The <script> Tag • In HTML, JavaScript code is inserted between <script> and </script> tags. <!DOCTYPE html> <html> <body> <h2>JavaScript in Body</h2> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = "My First JavaScript"; </script> </body> </html>
  • 6. NOTE: A JavaScript function is a block of JavaScript code, that can be executed when "called" for. For example, a function can be called when an event occurs, like when the user clicks a button. You can place any number of scripts in an HTML document. Scripts can be placed in the <body>, or in the <head> section of an HTML page, or in both. In following example (next slide), a JavaScript function is placed in the <head> section of an HTML page. The function is invoked (called) when a button is clicked:
  • 7. My First JavaScript <!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>
  • 8. JavaScript Output  JavaScript can "display" data in different ways: i. Writing into an HTML element, using innerHTML or innerText. ii. Writing into the HTML output using document.write(). iii. Writing into an alert box, using window.alert(). iv. Writing into the browser console, using console.log().
  • 9. Using innerHTML  To access an HTML element, you can use the document. getElementById (id) method.  Use the id attribute to identify the HTML element.  Then use the innerHTML property to change the HTML content of the HTML element:  Changing the innerHTML property of an HTML element is the most common way to display data in HTML
  • 10. Using innerHTML- EXAMPLE <!DOCTYPE html> <html><body> <h1>My Web Page</h1> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = "<h2>Hello World</h2>"; </script> </body></html>
  • 11. Using innerText  To access an HTML element, use the document. getElementById (id) method.  Then use the innerText property to change the inner text of the HTML element:  The document.write() method should only be used for testing.
  • 12. Using innerHTML- EXAMPLE <!DOCTYPE html> <html> <body> <h2>My First Web Page</h2> <p>My first paragraph.</p> <button type="button" onclick="document.write(5 + 6)">Try it</button> </body> </html>
  • 13. Using window.alert()  You can use an alert box to display data.  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:
  • 14. Using window.alert()- EXAMPLE <!DOCTYPE html> <html><body> <h1>My First Web Page</h1> <p>My first paragraph.</p> <script> window.alert(5 + 6); </script> </body></html> <!DOCTYPE html> <html><body> <h1>My First Web Page</h1> <p>My first paragraph.</p> <script> alert(5 + 6); </script> </body></html> EXAMPLE 1 EXAMPLE 2
  • 15. Using console.log() For debugging purposes, you can call the console.log() method in the browser to display data.
  • 16. Using console.log()- EXAMPLE <!DOCTYPE html> <html><body> <h2>Activate Debugging</h2> <p>F12 on your keyboard will activate debugging.</p> <p>Then select "Console" in the debugger menu.</p><p>Then click Run again.</p> <script> console.log(5 + 6); </script> </body></html>
  • 17. JavaScript Print  JavaScript does not have any print object or print methods.  You cannot access output devices from JavaScript.  The only exception is that you can call the window.print() method in the browser to print the content of the current window.
  • 18. JavaScript Print- EXAMPLE <!DOCTYPE html> <html> <body> <h2>The window.print() Method</h2> <p>Click the button to print the current page.</p> <button onclick="window.print()">Print this page</button> </body> </html>
  • 19. JavaScript Statements  A computer program is a list of "instructions" to be "executed" by a computer.  In a programming language, these programming instructions are called statements.  A JavaScript program is a list of programming statements.  In HTML, JavaScript programs are executed by the web browser.  JavaScript statements are composed of: Values, Operators, Expressions, Keywords, and Comments. Eg. document.getElementById("demo").innerHTML = "Hello Dolly.";  This statement tells the browser to write "Hello Dolly." inside an HTML element with id="demo":
  • 20. JavaScript Statements  Most JavaScript programs contain many JavaScript statements.  The statements are executed, one by one, in the same order as they are written.  JavaScript programs (and JavaScript statements) are often called JavaScript code. Semicolons ;  Semicolons separate JavaScript statements.  Add a semicolon at the end of each executable statement: eg. let a, b, c; // Declare 3 variables a = 5; // Assign the value 5 to a b = 6; // Assign the value 6 to b c = a + b; // Assign the sum of a and b to c  When separated by semicolons, multiple statements on one line are allowed: a = 5; b = 6; c = a + b;
  • 21. Semicolons ;  On the web, you might see examples without semicolons.  Ending statements with semicolon is not required, but highly recommended. JavaScript White Space  JavaScript ignores multiple spaces. You can add white space to your script to make it more readable.  The following lines are equivalent: let person = "Hege"; let person="Hege";  A good practice is to put spaces around operators ( = + - * / ): e.g. let x = y + z;
  • 22. JavaScript Statements- EXAMPLE 1 <!DOCTYPE html> <html><body><h2>JavaScript Statements</h2> <p>A <b>JavaScript program</b> is a list of <b>statements</b> to be executed by a computer.</p> <p id="demo"></p><script> let x, y, z; // Statement 1 x = 5; // Statement 2 y = 6; // Statement 3 z = x + y; // Statement 4 document.getElementById("demo").innerHTML = "The value of z is " + z + "."; </script></body></html>
  • 23. JavaScript Statements- EXAMPLE 2 <!DOCTYPE html> <html><body> <h2>JavaScript Statements</h2> <p>In HTML, JavaScript statements are executed by the browser.</p> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = "Hello Dolly."; </script> </body> </html>
  • 24. JavaScript Line Length and Line Breaks  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:.
  • 25. JavaScript Line Length and Line Breaks- EXAMPLE <!DOCTYPE html> <html><body> <h2>JavaScript Statements</h2> <p> The best place to break a code line is after an operator or a comma.</p> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = "Hello Dolly!"; </script> </body></html>
  • 26. JavaScript Code Blocks  JavaScript statements can be grouped together in code blocks, inside curly brackets {...}.  The purpose of code blocks is to define statements to be executed together.  One place you will find statements grouped together in blocks, is in JavaScript functions: function myFunction() { document.getElementById("demo1").innerHTML = "Hello Dolly!"; document.getElementById("demo2").innerHTML = "How are you?"; }
  • 27. JavaScript Code Blocks- EXAMPLE <!DOCTYPE html> <html><body><h2>JavaScript Statements</h2><p>JavaScript code blocks are written between { and }</p> <button type="button" onclick="myFunction()"> Click Me! </button> <p id="demo1"></p><p id="demo2"></p><script> function myFunction() { document.getElementById("demo1").innerHTML = "Hello Dolly!"; document.getElementById("demo2").innerHTML = "How are you?"; } </script></body></html>
  • 28. JavaScript Keywords  JavaScript statements often start with a keyword to identify the JavaScript action to be performed.  JavaScript keywords are reserved words. Reserved words cannot be used as names for variables.
  • 29. JavaScript Keywords KEYWORD DESCRIPTION var Declares a variable let Declares a block variable const Declares a block constant if Marks a block of statements to be executed on a condition switch Marks a block of statements to be executed in different cases for Marks a block of statements to be executed in a loop function Declares a function return Exits a function try Implements error handling to a block of statements
  • 30. JavaScript Keywords  JavaScript statements often start with a keyword to identify the JavaScript action to be performed.  JavaScript keywords are reserved words. Reserved words cannot be used as names for variables.