SlideShare a Scribd company logo
1 of 17
Introduction to Javascript
Module 1
What is JavaScript?
● JavaScript was initially created to “make web pages alive”.
● The programs in this language are called scripts. They can be written right in a web
page’s HTML and run automatically as the page loads.
● Scripts are provided and executed as plain text. They don’t need special preparation or
compilation to run.
● In this aspect, JavaScript is very different from another language called Java.
● JavaScript can execute not only in the browser, on any device that has a special
program called the JavaScript engine.
Why Js
● 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
Where to write js code ?
● script tag should be placed in HTML page's head
● script code is stored in a separate .js file
● JS code can be placed directly in the HTML file's body or head (like CSS)
but this is bad style (should separate content, presentation, and behavior)
<script src="filename" type="text/javascript"> script(s) </script>
HTML
Comments (same as Java)
// single-line comment
/* multi-line comment */
JS
identical to Java's comment syntax
4 comment syntaxes
HTML: <!-- comment -->
CSS/JS/PHP: /* comment */
Java/JS/PHP: // comment
PHP: # comment
Script 1:
Use of alert box
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript 1</title>
<script>
alert('Hello, I am ____!');
</script>
</head>
<body>
</body>
</html>
Using document.write() - This simply prints the specified text or HTML content
to the page.
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<script>
document.write(5 + 6);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<script>
document.write("<b>I am new text</b>");
</script>
</body>
</html>
String in webpage HTML tag in webpage
Using document.writeln() -method is identical to the write() method, with
the addition of writing a newline character after each statement.
<!DOCTYPE html>
<html>
<body>
<script>
document.write(5 + 6);
document.write(“<b> my lucky
number</b>”);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<script>
document.writeln(5 + 6);
document.writeln(“<b> my lucky
number</b>”);
</script>
</body>
</html>
document.write document.writeln
4 Ways to Declare
a JavaScript
Variable:
var keyword
var x = 5;
var y = 6;
var z = x + y;
let keyword
let x = 5;
let y = 6;
let z = x + y;
undeclared variables
x = 5;
y = 6;
z = x + y;
Scope of var and let
<!DOCTYPE html>
<html>
<body>
<h2>Redeclaring a Variable Using let</h2>
<script>
let x = 10;
document.writeln(x); {
let x = 2;
document.writeln(x); }
document.writeln(x);
</script> </body> </html>
var x = 10;
// Here x is 10
{
var x = 2;
// Here x is 2
}
// Here x is 2
let var
Declare a variable with
const unless you know that
the value will change.
const num1 = 5;
const num2 = 3;
// add two numbers
const sum = num1 + num2;
// display the sum
document.write('The sum of ' + num1 + ' and ' + num2
+ ' is: ' + sum);
JavaScript Popup Boxes
JavaScript has three kind of popup boxes: Alert box, Confirm box, and Prompt box.
An alert box is often used if you
want to make sure information
comes through to the user.
When an alert box pops up, the
user will have to click "OK" to
proceed.
<!DOCTYPE html>
<html>
<body> <h2>Alert Example</h2>
<button onclick="displayBox()">Magic</button>
<script>
function displayBox() {
alert("I am an alert box!n Nice to meet you"); }
</script>
</body>
</html>
A confirm box is often used
if you want the user to verify
or accept something.
When a confirm box pops
up, the user will have to click
either "OK" or "Cancel" to
proceed.
If the user clicks "OK", the
box returns true. If the user
clicks "Cancel", the box
returns false.
<!DOCTYPE html>
<html>
<body>
<h2>Confirm Box</h2>
<button onclick="typeReply()">Check</button>
<p id="display"></p> <script>
function typeReply() {
var txt;
if (confirm("Would you like to order tea?")) {
txt = "Tea is ordered";
} else {
txt = "You pressed Cancel!";
}
document.getElementById("display").innerHTML = txt;
}
</script>
</body>
</html>
A prompt box is often used if
you want the user to input a
value before entering a page.
When a prompt box pops up,
the user will have to click
either "OK" or "Cancel" to
proceed after entering an
input value.
If the user clicks "OK" the
box returns the input value.
If the user clicks "Cancel" the
box returns null.
<!DOCTYPE html>
<html>
<body>
<h2>Prompt</h2>
<button onclick="userName()">Try it</button>
<p id="demo"></p>
<script>
function userName() {
let text;
let person = prompt("Please enter your name:", "Richie");
if (person == null || person == "") {
text = "Empty";
} else {
text = "Hello " + person + "! How are you today?";
}
document.getElementById("demo").innerHTML = text;
}
</script></body></html>
References
https://javascript.info/intro#what-is-javascript

More Related Content

Similar to Java Script (Module 1).pptx

Essential Javascript -- A Javascript &lt;b>Tutorial&lt;/b>
Essential Javascript -- A Javascript &lt;b>Tutorial&lt;/b>Essential Javascript -- A Javascript &lt;b>Tutorial&lt;/b>
Essential Javascript -- A Javascript &lt;b>Tutorial&lt;/b>tutorialsruby
 
Essential_Javascript_--_A_Javascript_Tutorial
Essential_Javascript_--_A_Javascript_TutorialEssential_Javascript_--_A_Javascript_Tutorial
Essential_Javascript_--_A_Javascript_Tutorialtutorialsruby
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />tutorialsruby
 
Essential_Javascript_--_A_Javascript_Tutorial
Essential_Javascript_--_A_Javascript_TutorialEssential_Javascript_--_A_Javascript_Tutorial
Essential_Javascript_--_A_Javascript_Tutorialtutorialsruby
 
Javascript alert and confrim box
Javascript alert and confrim boxJavascript alert and confrim box
Javascript alert and confrim boxJesus Obenita Jr.
 
JavaScript with Syntax & Implementation
JavaScript with Syntax & ImplementationJavaScript with Syntax & Implementation
JavaScript with Syntax & ImplementationSoumen Santra
 
Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4DanWooster1
 
Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4DanWooster1
 
Introduction to java script
Introduction to java scriptIntroduction to java script
Introduction to java scriptnanjil1984
 
Js placement
Js placementJs placement
Js placementSireesh K
 
JavaScript - Getting Started.pptx
JavaScript - Getting Started.pptxJavaScript - Getting Started.pptx
JavaScript - Getting Started.pptxJonnJorellPunto
 

Similar to Java Script (Module 1).pptx (20)

Essential Javascript -- A Javascript &lt;b>Tutorial&lt;/b>
Essential Javascript -- A Javascript &lt;b>Tutorial&lt;/b>Essential Javascript -- A Javascript &lt;b>Tutorial&lt;/b>
Essential Javascript -- A Javascript &lt;b>Tutorial&lt;/b>
 
Essential_Javascript_--_A_Javascript_Tutorial
Essential_Javascript_--_A_Javascript_TutorialEssential_Javascript_--_A_Javascript_Tutorial
Essential_Javascript_--_A_Javascript_Tutorial
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
Essential_Javascript_--_A_Javascript_Tutorial
Essential_Javascript_--_A_Javascript_TutorialEssential_Javascript_--_A_Javascript_Tutorial
Essential_Javascript_--_A_Javascript_Tutorial
 
Javascript alert and confrim box
Javascript alert and confrim boxJavascript alert and confrim box
Javascript alert and confrim box
 
Javascript
JavascriptJavascript
Javascript
 
lect4
lect4lect4
lect4
 
lect4
lect4lect4
lect4
 
JavaScript with Syntax & Implementation
JavaScript with Syntax & ImplementationJavaScript with Syntax & Implementation
JavaScript with Syntax & Implementation
 
Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4
 
Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4
 
JAVA SCRIPT
JAVA SCRIPTJAVA SCRIPT
JAVA SCRIPT
 
Introduction to java script
Introduction to java scriptIntroduction to java script
Introduction to java script
 
wp-UNIT_III.pptx
wp-UNIT_III.pptxwp-UNIT_III.pptx
wp-UNIT_III.pptx
 
Lecture6
Lecture6Lecture6
Lecture6
 
Basics
BasicsBasics
Basics
 
Wt unit 5
Wt unit 5Wt unit 5
Wt unit 5
 
Unit 2.4
Unit 2.4Unit 2.4
Unit 2.4
 
Js placement
Js placementJs placement
Js placement
 
JavaScript - Getting Started.pptx
JavaScript - Getting Started.pptxJavaScript - Getting Started.pptx
JavaScript - Getting Started.pptx
 

More from Shehrevar Davierwala (20)

Introduction_Swift
Introduction_SwiftIntroduction_Swift
Introduction_Swift
 
PsudoCode.pptx
PsudoCode.pptxPsudoCode.pptx
PsudoCode.pptx
 
Number System.pptx
Number System.pptxNumber System.pptx
Number System.pptx
 
Website in Clicks Day 2
Website in Clicks Day 2Website in Clicks Day 2
Website in Clicks Day 2
 
Develop Website in Clicks
Develop Website in ClicksDevelop Website in Clicks
Develop Website in Clicks
 
Build Virtual Assistant Using AI
Build Virtual Assistant Using AI Build Virtual Assistant Using AI
Build Virtual Assistant Using AI
 
Build brand reputation using facebook
Build brand reputation using facebookBuild brand reputation using facebook
Build brand reputation using facebook
 
Digital Marketing Session 2
Digital Marketing Session 2Digital Marketing Session 2
Digital Marketing Session 2
 
Learn Digital Marketing : 0 to Hero Day 1
Learn Digital Marketing :  0 to Hero Day 1 Learn Digital Marketing :  0 to Hero Day 1
Learn Digital Marketing : 0 to Hero Day 1
 
Standard template
Standard templateStandard template
Standard template
 
Digital Marketing for Sustainable Business - Afghan Perspective
Digital Marketing for Sustainable Business - Afghan Perspective  Digital Marketing for Sustainable Business - Afghan Perspective
Digital Marketing for Sustainable Business - Afghan Perspective
 
Developing stunning website in clicks - 2
Developing stunning website in clicks - 2Developing stunning website in clicks - 2
Developing stunning website in clicks - 2
 
Developing stunning website in clicks
Developing stunning website in clicksDeveloping stunning website in clicks
Developing stunning website in clicks
 
Google forms for data analysis
Google forms for data analysisGoogle forms for data analysis
Google forms for data analysis
 
Webdesign session1
Webdesign session1Webdesign session1
Webdesign session1
 
Tech talk webtech
Tech talk webtechTech talk webtech
Tech talk webtech
 
Tech talk php_cms
Tech talk php_cmsTech talk php_cms
Tech talk php_cms
 
Ph pbasics
Ph pbasicsPh pbasics
Ph pbasics
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
Java operators
Java operatorsJava operators
Java operators
 

Recently uploaded

Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 

Recently uploaded (20)

Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 

Java Script (Module 1).pptx

  • 2. What is JavaScript? ● JavaScript was initially created to “make web pages alive”. ● The programs in this language are called scripts. They can be written right in a web page’s HTML and run automatically as the page loads. ● Scripts are provided and executed as plain text. They don’t need special preparation or compilation to run. ● In this aspect, JavaScript is very different from another language called Java. ● JavaScript can execute not only in the browser, on any device that has a special program called the JavaScript engine.
  • 3.
  • 4. Why Js ● 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
  • 5.
  • 6. Where to write js code ? ● script tag should be placed in HTML page's head ● script code is stored in a separate .js file ● JS code can be placed directly in the HTML file's body or head (like CSS) but this is bad style (should separate content, presentation, and behavior) <script src="filename" type="text/javascript"> script(s) </script> HTML
  • 7. Comments (same as Java) // single-line comment /* multi-line comment */ JS identical to Java's comment syntax 4 comment syntaxes HTML: <!-- comment --> CSS/JS/PHP: /* comment */ Java/JS/PHP: // comment PHP: # comment
  • 8. Script 1: Use of alert box <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>JavaScript 1</title> <script> alert('Hello, I am ____!'); </script> </head> <body> </body> </html>
  • 9. Using document.write() - This simply prints the specified text or HTML content to the page. <!DOCTYPE html> <html> <body> <h1>My First Web Page</h1> <p>My first paragraph.</p> <script> document.write(5 + 6); </script> </body> </html> <!DOCTYPE html> <html> <body> <h1>My First Web Page</h1> <p>My first paragraph.</p> <script> document.write("<b>I am new text</b>"); </script> </body> </html> String in webpage HTML tag in webpage
  • 10. Using document.writeln() -method is identical to the write() method, with the addition of writing a newline character after each statement. <!DOCTYPE html> <html> <body> <script> document.write(5 + 6); document.write(“<b> my lucky number</b>”); </script> </body> </html> <!DOCTYPE html> <html> <body> <script> document.writeln(5 + 6); document.writeln(“<b> my lucky number</b>”); </script> </body> </html> document.write document.writeln
  • 11. 4 Ways to Declare a JavaScript Variable: var keyword var x = 5; var y = 6; var z = x + y; let keyword let x = 5; let y = 6; let z = x + y; undeclared variables x = 5; y = 6; z = x + y;
  • 12. Scope of var and let <!DOCTYPE html> <html> <body> <h2>Redeclaring a Variable Using let</h2> <script> let x = 10; document.writeln(x); { let x = 2; document.writeln(x); } document.writeln(x); </script> </body> </html> var x = 10; // Here x is 10 { var x = 2; // Here x is 2 } // Here x is 2 let var
  • 13. Declare a variable with const unless you know that the value will change. const num1 = 5; const num2 = 3; // add two numbers const sum = num1 + num2; // display the sum document.write('The sum of ' + num1 + ' and ' + num2 + ' is: ' + sum);
  • 14. JavaScript Popup Boxes JavaScript has three kind of popup boxes: Alert box, Confirm box, and Prompt box. An alert box is often used if you want to make sure information comes through to the user. When an alert box pops up, the user will have to click "OK" to proceed. <!DOCTYPE html> <html> <body> <h2>Alert Example</h2> <button onclick="displayBox()">Magic</button> <script> function displayBox() { alert("I am an alert box!n Nice to meet you"); } </script> </body> </html>
  • 15. A confirm box is often used if you want the user to verify or accept something. When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed. If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false. <!DOCTYPE html> <html> <body> <h2>Confirm Box</h2> <button onclick="typeReply()">Check</button> <p id="display"></p> <script> function typeReply() { var txt; if (confirm("Would you like to order tea?")) { txt = "Tea is ordered"; } else { txt = "You pressed Cancel!"; } document.getElementById("display").innerHTML = txt; } </script> </body> </html>
  • 16. A prompt box is often used if you want the user to input a value before entering a page. When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input value. If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box returns null. <!DOCTYPE html> <html> <body> <h2>Prompt</h2> <button onclick="userName()">Try it</button> <p id="demo"></p> <script> function userName() { let text; let person = prompt("Please enter your name:", "Richie"); if (person == null || person == "") { text = "Empty"; } else { text = "Hello " + person + "! How are you today?"; } document.getElementById("demo").innerHTML = text; } </script></body></html>