CLASS 11 Jav Script Notes
CLASS 11 Jav Script Notes
</SCRIPT>
Table: list of reserved keywords in JavaScript
Q- Write a program in JavaScript to swap two variable values
<script>
let a = prompt('Enter the first variable: ');
let b = prompt('Enter the second variable: ');
let temp;
temp = a;
a = b;
b = temp;
document.write(`The value of a after swapping: ${a}`);
document.write(`The value of b after swapping: ${b}`);
</script>
document.write(2);
document.write(2 + 3); result 5
document.write(3 - 2); result 1
document.write(2 * 3); result 6
document.write(6 /3); result 3
document.write(a); will result a = a + 1 means 5 + 1 = 6
document.write(a); will result a = a - 1 means 5 - 1 = 4
document.write(c);
</script>
<script>
var a = 6;
var b = 2;
a +=b; means a = a + b ie ans = 6 + 2 = 8
a -=b; means a = a - b ie ans = 6 - 2 = 4
a *=b; means a = a * b ie ans = 6 * 2 = 12
a /=b; means a = a / b ie ans = 6 / 2 = 3
document.write(a);
</script>
<script>
var a = 5;
var b = 6;
console.log(a==b); result true
console.log(a===b);result
console.log(a!=b);check result
console.log(a!==); check result
console.log(a>b); check result
console.log(a<b); check result
console.log(a>=b); check result
console.log(a<=b); check result
</script>
LOGICAL OPERATOR
LOGICAL AND
LOGICAL OR 45 OR 17
||
LOGICAL (NOT)
CONDITIONAL OPERATOR
typeof operator
The typeof is a unary operator that is placed before a single
operand, which can be of any type. its value is a string indicating
the data type of the operand. The type of operator evaluates to
“number”, “string”, or “Boolean” if its operand is a number,
string, or Boolean value and returns true or false based on the
evaluation. here is the list of return values for the type of
operator:
SESSION -4
OUTPUT :FALSE
OUTPUT :TRUE
(USING IF FUNCTION )
Numerical Calculation:
Math.round(x): It returns the rounded value of passing number
to its nearest integer. For example:
Math.round(6.7) //returns 7
Math.round(6.4) //returns 6
Math.ceil();
Maths.floor();
Math.pow();
<script>
var a = Math.pow(2,3);
document.write(a);
</script>
Output : 8
Math.sqrt();
<script>
var a = Math.sqrt(9);
document.write(a);
</script>
Basic String Operations
1. The .length(); property returns the length of a string:
2. The charAt(); method returns the character at a specified index
(position) in a string:
3. The charCodeAt() method returns the code of the character at a
specified index in a string:
4. slice() extracts a part of a string and returns the extracted part in a
new string.
5. <script>
6. let text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
7.
8. let texts = "Apple, Banana, Kiwi";
9.
10. document.write(text.charCodeAt(0));
11. document.write("<br>");
12. document.write(text.length);
13. document.write("<br>");
14. document.write(text.charAt(0));
15. document.write("<br>");
16. document.write(name.at(2));
17. document.write("<br>");
18. document.write(texts.slice(7,13));
19. document.write("<br>");
20. document.write(texts.substring(7,13));
21.
22. </script>
Section-5: Decision Making using if and switch
USING IF STATEMENT
USING IF ELSE STATEMENT
SWITCH STATEMENT
The switch statement is used to select one of many blocks of
the code to be executedin a program described below. the
switch expression is evaluated once and the valueof the
expression is compared with the values of each case. if there
is a match found, then the associated block of code is
executed otherwise default code block is executed.
<script>
var month = 22;
switch(month){
case(1):document.write("january");
break;
case(2):document.write("february");
break;
case(3):document.write("march");
break;
case(4):document.write("april");
break;
case(5):document.write("may");
break;
case(6):document.write("june");
break;
case(7):document.write("july");
break;
default:document.write("plz enter the valid month");
}
</script>
Section-6: Looping Structure
Loops can execute a block of code a number of times.
TYPES OF LOOP
</script>
Result : 1 2 3 4 5
While loop
The While loop is another commonly used loop in
JavaScript. the purpose of the while loop is to execute a
block of statements over and over again until the condition
fails.
<script>
let i = 1;
while(i<=5){
console.log(i);
</script>
Result : infinite times
<script>
let i = 1;
while(i<=5){
console.log(i);
i++;
}
</script>
Output : 1 2 3 4 5
DO WHILE LOOP
This is another kind of loop and different from the for loop
and the while loop. This loop will execute the statement at
least once that is the statements inside the loop will always
get executed at least once, even if the condition is false.
<script>
let i = 10;
do{
document.write(i + '<br>');
i++;
}while(i<=5);
</script>
OUTPUT : 10