Unit IV - WT
Unit IV - WT
7. </script>
8. </head>
9. <body>
10.<p>Inthis example we saw how to add JavaScript in the head section </p>
11.</body>
12.</html>
Output:
We can also define the JavaScript code in the <body> tags or body section.
Let's understand through an example.
1. <!DOCTYPE html >
2. <html>
3. <head>
4. <title> page title</title>
5. </head>
6. <body>
7. <script>
8. document.write("Welcome to Javatpoint");
9. </script>
10.<p> In this example we saw how to add JavaScript in the body section </p>
11.</body>
12.</html>
Output
Both of the above programs are saved in the same folder, but you can also store
JavaScript code in a separate folder, all just you need to provide the address/path of
the (.js) file in the src attribute of <script> tag.
JavaScript Variable
1. JavaScript variable
2. JavaScript Local variable
3. JavaScript Global variable
A JavaScript variable is simply a name of storage location. There are two types
of variables in JavaScript : local variable and global variable.
There are some rules while declaring a JavaScript variable (also known as
identifiers).
1. var x = 10;
2. var _value="sonoo";
DEPARTMENT OF COMPUTERSCIENCE-RASC/R.SUGANYA, AP Page 5
23UCSCC53 – WEB TECHNOLOGY - UNIT 4
1. var 123=30;
2. var *aa=320;
1. <script>
2. var x = 10;
3. var y = 20;
4. var z=x+y;
5. document.write(z);
6. </script>
Output of the above example
30
1. <script>
2. function abc(){
3. var x=10;//local variable
4. }
5. </script>
Or,
1. <script>
2. If(10<13){
3. var y=20;//JavaScript local variable
4. }
5. </script>
1. <script>
2. var data=200;//gloabal variable
3. function a(){
4. document.writeln(data);
5. }
6. function b(){
7. document.writeln(data);
8. }
9. a();//calling JavaScript function
10.b();
11.</script>
200 200
Javascript Data Types
JavaScript provides different data types to hold different types of values. There
are two types of data types in JavaScript.
1. Primitive data type
2. Non-primitive (reference) data type
JavaScript is a dynamic type language, means you don't need to specify type of
the variable because it is dynamically used by JavaScript engine. You need to
use var here to specify the data type. It can hold any type of values such as
numbers, strings etc. For example:
1. var a=40;//holding number
2. var b="Rahul";//holding string
JavaScript primitive data types
There are five types of primitive data types in JavaScript. They are as follows:
Number
represents numeric values e.g. 100
JavaScript Operators
JavaScript operators are symbols that are used to perform operations on operands.
For example:
1. var sum=10+20;
Here, + is the arithmetic operator and = is the assignment operator.
There are following types of operators in JavaScript.
1. Arithmetic Operators
2. Comparison (Relational) Operators
3. Bitwise Operators
4. Logical Operators
5. Assignment Operators
6. Special Operators
+ Addition 10+20 = 30
- Subtraction 20-10 = 10
/ Division 20/10 = 2
false
~ Bitwise NOT
(~10) = -10
(10==20 &&
&& Logical AND
20==33) = false
|| (10==20 ||
Logical OR
20==33) = false
= Assign
10+10 = 20
Operator Description
CONTROL STRUCTURES
Control structures are programmatic constructs that determine how your code
executes. They provide mechanisms for making decisions, repeating code
blocks, and changing the flow of execution based on specific conditions. By
effectively utilizing these structures, you can write code that is more responsive,
adaptable, and efficient.
JavaScript offers a rich set of control structures, categorized into three primary
types:
Conditional Statements: These structures allow your code to make
decisions based on conditions. They execute specific code blocks only when
the defined conditions are met.
Looping Statements: These structures enable you to repeat a block of code
a specific number of times or until a particular condition is met. This is
crucial for iterating through data collections and performing repetitive tasks.
Jumping Statements: These structures alter the normal flow of execution
within a loop or conditional statement. They provide mechanisms for exiting
loops before the loop stops or skipping iterations.
Understanding how these structures work individually and how they can be
combined effectively is key to writing robust and well-structured JavaScript code.
JavaScript If-else
The JavaScript if-else statement is used to execute the code whether condition is
true or false. There are three forms of if statement in JavaScript.
1. If Statement
2. If else statement
3. if else if statement
JavaScript If statement
1. if(expression){
2. //content to be evaluated
3. }
1. <script>
2. var a=20;
3. if(a>10){
4. document.write("value of a is greater than 10");
5. }
6. </script>
Output of the above example
value of a is greater than 10
It evaluates the content whether condition is true of false. The syntax of JavaScript
if-else statement is given below.
1. if(expression){
2. //content to be evaluated if condition is true
3. }
4. else{
5. //content to be evaluated if condition is false
6. }
Let’s see the example of if-else statement in JavaScript to find out the even or odd
number.
1. <script>
2. var a=20;
3. if(a%2==0){
4. document.write("a is even number");
5. }
6. else{
7. document.write("a is odd number");
8. }
9. </script>
Output of the above example
a is even number
It evaluates the content only if expression is true from several expressions. The
signature of JavaScript if else if statement is given below.
1. if(expression1){
2. //content to be evaluated if expression1 is true
3. }
4. else if(expression2){
5. //content to be evaluated if expression2 is true
6. }
7. else if(expression3){
8. //content to be evaluated if expression3 is true
9. }
10.else{
11.//content to be evaluated if no expression is true
12.}
Let’s see the simple example of if else if statement in javascript.
1. <script>
2. var a=20;
3. if(a==10){
4. document.write("a is equal to 10");
5. }
6. else if(a==15){
7. document.write("a is equal to 15");
8. }
9. else if(a==20){
10.document.write("a is equal to 20");
11.}
12.else{
13.document.write("a is not equal to 10, 15 or 20");
14.}
15.</script>
Output of the above example
a is equal to 20
JavaScript Switch
The JavaScript switch statement is used to execute one code from multiple
expressions. It is just like else if statement that we have learned in previous page.
But it is convenient than if..else..if because it can be used with numbers, characters
etc.
The signature of JavaScript switch statement is given below.
1. switch(expression){
2. case value1:
3. code to be executed;
4. break;
5. case value2:
6. code to be executed;
7. break;
8. ......
9.
10.default:
11. code to be executed if above values are not matched;
12.}
Let’s see the simple example of switch statement in javascript.
1. <script>
2. var grade='B';
3. var result;
4. switch(grade){
5. case 'A':
6. result="A Grade";
7. break;
8. case 'B':
9. result="B Grade";
10.break;
11.case 'C':
12.result="C Grade";
13.break;
14.default:
15.result="No Grade";
16.}
17.document.write(result);
18.</script>
1. while (condition)
2. {
DEPARTMENT OF COMPUTERSCIENCE-RASC/R.SUGANYA, AP Page 19
23UCSCC53 – WEB TECHNOLOGY - UNIT 4
3. code to be executed
4. }
Let’s see the simple example of while loop in javascript.
1. <script>
2. var i=11;
3. while (i<=15)
4. {
5. document.write(i + "<br/>");
6. i++;
7. }
8. </script>
Output:
11
12
13
14
15
3) JavaScript do while loop
The JavaScript do while loop iterates the elements for the infinite number of
times like while loop. But, code is executed at least once whether condition is true
or false. The syntax of do while loop is given below.
1. do{
2. code to be executed
3. }while (condition);
Let’s see the simple example of do while loop in javascript.
1. <script>
2. var i=21;
3. do{
4. document.write(i + "<br/>");
5. i++;
6. }while (i<=25);
7. </script>
Output:
21
22
23
24
25
JavaScript Functions
Let’s see the simple example of function in JavaScript that does not has arguments.
1. <script>
2. function msg(){
3. alert("hello! this is message");
4. }
5. </script>
6. <input type="button" onclick="msg()" value="call function"/>
Output of the above example
We can call function by passing arguments. Let’s see the example of function that
has one argument.
1. <script>
2. function getcube(number){
3. alert(number*number*number);
4. }
5. </script>
6. <form>
7. <input type="button" value="click" onclick="getcube(4)"/>
8. </form>
Output of the above example
We can call function that returns a value and use it in our program. Let’s see the
example of function that returns value.
1. <script>
2. function getInfo(){
3. return "hello javatpoint! How r u?";
4. }
5. </script>
6. <script>
7. document.write(getInfo());
8. </script>
Output of the above example
hello javatpoint! How r u?
Syntax
Parameter
Method Description
Example 1
1. <script>
2. var add=new Function("num1","num2","return num1+num2");
3. document.writeln(add(2,5));
4. </script>
Output:
Example 2
1. <script>
2. var pow=new Function("num1","num2","return Math.pow(num1,num2)");
3. document.writeln(pow(2,3));
4. </script>
Output:
2.
3. <head>
4. <script type="text/javascript">
5. function show() {
6. alert("It is an Alert dialog box");
7. }
8. </script>
9. </head>
10.
11.<body>
12. <center>
13. <h1>Hello World :) :)</h1>
14. <h2>Welcome to javaTpoint</h2>
15. <p>Click the following button </p>
16. <input type="button" value="Click Me" onclick="show();" />
17. </center>
18.</body>
19.
20.</html>
Output
After the successful execution of the above code, you will get the following output.
After clicking on the Click Me button, you will get the following output:
18. <center>
19. <h1>Hello World :) :)</h1>
20. <h2>Welcome to javaTpoint</h2>
21. <p>Click the following button </p>
22. <input type="button" value="Click Me" onclick="show();" />
23. </center>
24.</body>
25.
26.</html>
Output
After the successful execution of the above code, you will get the following output.
When you click on the given button, then you will get the following output:
The prompt dialog box is used when it is required to pop-up a text box for getting
the user input. Thus, it enables interaction with the user.
The prompt dialog box also has two buttons, which are OK and Cancel. The user
needs to provide input in the textbox and then click OK. When a user clicks on the
OK button, then the dialog box reads that value and returns it to the user. But on
clicking the Cancel button, prompt() method returns null.
Syntax
1. prompt(message, default_string);
Let us understand the prompt dialog box by using the following illustration.
Example
1. <html>
2.
3. <head>
4. <script type="text/javascript">
5. function show() {
6. var value = prompt("Enter your Name : ", "Enter your name");
7. document.write("Your Name is : " + value);
8. }
9. </script>
10.</head>
11.
12.<body>
13. <center>
14. <h1>Hello World :) :)</h1>
15. <h2>Welcome to javaTpoint</h2>
16. <p>Click the following button </p>
17. <input type="button" value="Click Me" onclick="show();" />
18. </center>
19.</body>
20.
21.</html>
Output
After executing the above code successfully, you will get the following output.
When you click on the Click Me button, you will get the following output: