In most of the JavaScript programs, which we learned so far, the lines of code were executed in the same order in which they appeared in the program. Each code was executed only once. Thus, the code did not include tests to determine if the conditions were true or false or we did not perform any logical statements.
In this chapter, you are going to learn some logical programming. You will learn about the following topics:
You already know how to embed JavaScript codes on an HTML document. Before starting this chapter, you will learn a few HTML tags and JavaScript methods. These methods and tags will be used throughout the book.
In the previous chapters, you learned how to print something using document.write()
. Now, you will learn something more.
We will check the methods on both console and HTML document, as follows:
alert("Hello World");
Type this on the console and press Enter, you will see a pop-up box saying Hello World:
You can write your code to show a pop-up box similar to the following on an HTML document:
<html> <head> <title>Alert</title> </head> <body> <script type="text/javascript"> alert("Hello World"); </script> </body> </html>
The output will be as follows:
window.prompt()
method.window.prompt()
is similar to the following:window.prompt("What is your name?"); // You can type anything between the inverted commas.
var name = window.prompt("what is your name?");
name
variable:document.write();
method, as follows:document.write("Hello "+name+"!");
<html> <head> <title>Prompt</title> </head> <body> <script type="text/javascript"> var name = window.prompt("What is your name?"); document.write("Hello "+name+"!"); </script> </body> </html>
3.144.189.177