0% found this document useful (0 votes)
42 views38 pages

CLASS 11 Jav Script Notes

The document provides an overview of data types, variables, operators, and control structures in JavaScript, including examples of how to use them. It covers built-in functions for numerical calculations, string operations, decision-making using if and switch statements, and looping structures like for, while, and do-while loops. Each section includes code snippets to illustrate the concepts discussed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views38 pages

CLASS 11 Jav Script Notes

The document provides an overview of data types, variables, operators, and control structures in JavaScript, including examples of how to use them. It covers built-in functions for numerical calculations, string operations, decision-making using if and switch statements, and looping structures like for, while, and do-while loops. Each section includes code snippets to illustrate the concepts discussed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Section-2: Data Types and Variables

Data Types : A data type is a classification of the type of data


that a variable or an object can hold data type is an important
factor in all computer programming languages.
Ex- <SCRIPT>
var firstname="java script";
document.write("My name is" );
document.write(firstname);

</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>

Q- Write a program to print the smiley face


Section-3: Operators
<script>
var a = 5;
var b = 6;
var c = a+ b;
a++; increment operator
a- Decrement operator

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

Built-in Functions/Object for Numerical Calculation

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

EX-1 Make a “happy day” greeting if the hour is less than


18:00:

EX-2 if the hour is less than 18, create a “good day”


greeting, otherwise “good Evening”:
EX-3 Calculate discount price base on total amount. If
amount is less than 10000 then no discount. If amount is less
than 20000 then discount is 5%. If amount is less than 30000
then discount is 10%. If amount is more than 50000 then
discount is 15%.

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

Syntax : for(initialization, condition, increment opr);


for(initialization, condition, decrement opr);
<script>
for(let i=1; i<=5; i++){
document.write(i);
}

</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

You might also like