JavaScript
JavaScript
Javascript:
JavaScript is a powerful programming language that can add
interactively to a website.
A scripting language is a computer programming language that
provides instructions, called scripts, for software in apps and
websites.
Why we need js:
Its an event based programming lanagauge.
Click, double click, right click ,left click, drag and drop, key
press ,load etc..
Java is very versatile as it is used for programming
applications on the web, mobile, desktop, etc.
OUTPUT DISPLAY:
Document.write(“hello”); :its used to display some
string in the output of the html page .document.write
in js is methods or functions in js
Alter(“hii”) : this method is used to alter the message
.
Comsole.log(“welcome”); its build in function that
allows you to output message to console .
Its commomnly used in browder to debugging your
code.
JavaScript Statements:
A computer program is a list of "instructions" to be
"executed" by a computer.
In a programming language, these programming
instructions are called statements.
EXAMPLES:
"Hello Dolly."
Java script
JavaScript Comments:
JavaScript comments can be used to explain
JavaScript code, and to make it more readable.
JavaScript comments can also be used to prevent
execution, when testing alternative code.
Single Line Comments:
Multi-line Comments
Multi-line Comments:
Java script
It is represented by forward slash with asterisk then
asterisk with forward slash. For example:
/* your code here */
EXAMPLE :
<script>
/* It is multi line comment.
It will not be displayed */
document.write("example of javascript multiline comm
ent");
</script>
JavaScript Variables:
Variables are Containers for Storing Data
JavaScript Variables can be declared in 3 ways:
• Using var
• Using let
• Using const
CONSTANT VARIABLES:
Java script key word const is used tho declared the
constant values and cannot be reassigned the
values .once it declare the value it cant be change.
Example:
Pi = 3.14159;
JavaScript Operators:
There are different types of JavaScript operators:
• Arithmetic Operators
• Assignment Operators
• Comparison Operators
• String Operators
Java script
• Logical Operators
• Bitwise Operators
• Ternary Operators
Operator Description
+ Addition
- Subtraction
* Multiplication
** Exponentiation
/ Division
++ Increment
-- Decrement
Comparison Operators:
Comparison operators are used in logical statements
to determine equality or difference between variables
or values.
Examples:
if (age < 18) text = "Too young to buy alcohol";
== equal to
!= not equal
Logical Operators:
Logical operators are used to determine the logic
between variables or values.
Example :
Operator Description Example
|| Or (x == 5 || y == 5) is false
JavaScript String
A string represents textual data. It contains a sequence of characters. For
example, "hello" , "JavaScript" , etc.
In JavaScript, strings are surrounded by quotes:
For example:
For example:
JavaScript Number:
In JavaScript, the number type represents numeric values (both integers
and floating-point numbers).
Examples:
// integer value
let integer_number = -3;
console.log(integer_number);
// floating-point value
let float_number = 3.15;
console.log(float_number);
JavaScript BigInt :
BigInt is a type of number that can represent very large or very small
integers beyond the range of the regular number data type.
For example:
// BigInt value
let value1 = 900719925124740998n;
Java script
JavaScript Boolean
A Boolean data can only have one of two
values: true or false
JavaScript undefined:
let name;
console.log(name); // undefined
JavaScript null:
In JavaScript, null represents "no value" or "nothing."
For example:
Java script
let number = null;
console.log(number); // null
JavaScript Object
An Object holds data in the form of key-value pairs.
For example,
let student = {
firstName: "John",
lastName: null,
class: 10
};
JavaScript if Statement
We use the if keyword to execute code based on some specific
condition.
The syntax of if statement is:
if (condition) {
// block of code
}
Java script
The if keyword checks the condition inside the parentheses ().
If the condition is evaluated to true, the code inside { } is
executed.
If the condition is evaluated to false, the code inside { } is
skipped.
Example:
// Program to check if the number is positive
const number = 6;
console.log("nice number");
Example :
// if no condition matches
else {
// else body
}
if (rating <= 2) {
console.log("Bad rating");
}
else if (rating >= 4) {
console.log("Good rating!");
}
else {
console.log("Average rating");
}
else {
console.log("Failed");
}
// Output: Passed
In JavaScript, the for loop is used for iterating over a block of code
1
2
3
4
5
1 is pri
1st i=1 true
i is increased to 2.
2 is pri
2nd i=2 true
i is increased to 3.
3 is pri
3rd i=3 true
i is increased to 4.
4 is pri
4th i=4 true
i is increased to 5.
Java script
5 is pri
5th i=5 true
i is increased to 6.
while (condition) {
// body of loop
}
Output
1
2
3
Here is how the above program works in each iteration of the loop:
do {
// body of loop
} while(condition);
let i = 3;
// do...while loop
do {
console.log(i);
i--;
} while (i > 0);
Output
3
2
1
Output
1
2
The continue statement skips the current iteration of the loop and
// Output:
// 1
// 3
// 5
JavaScript switch...case
Statement:
Java script
switch (expression) {
case value1:
// code block to be executed
// if expression matches value1
break;
case value2:
// code block to be executed
// if expression matches value2
break;
...
default:
// code block to be executed
// if expression doesn't match any case
}
let day = 3;
let activity;
switch (day) {
case 1:
console.log("Sunday");
Java script
break;
case 2:
console.log("Monday");
break;
case 3:
console.log("Tuesday");
break;
case 4:
console.log("Wednesday");
break;
case 5:
console.log("Thursday");
break;
case 6:
console.log("Friday");
break;
case 7:
console.log("Saturday");
break;
default:
console.log("Invalid Day");
}
Output
Tuesday
while(condition){ do{
Syntax // loop body // loop body
} } while (condition);
JavaScript Objects:
value pair.
const objectName = {
key1: value1,
key2: value2,
...,
keyN: valueN
};
Here,
const dog = {
name: "Rocky",
};
// access property
console.log(dog.name);
JavaScript Array
An array is an object that can store multiple values at once.
In the above example, we created an array to record the age of five students.
Here, we've only listed a few fruits. But what if we need to store 100 fruits?
For such a case, the easiest solution is to store them in an array.
Create an Array
We can create an array by placing elements inside an array literal [] ,
separated by commas. For example,
Here,
new Date()
new Date(milliseconds)
new Date()
You can create a date object using the new Date() constructor. For example,
const timeNow = new Date();
console.log(timeNow); // shows current date and time
Output
Here, new Date() creates a new date object with the current date and local time.
Returns the numeric value corresponding to the current time (the number of milliseconds
now()
elapsed since January 1, 1970 00:00:00 UTC)
getDate() Gets the day of the month (1–31) according to local time
getDay() Gets the day of the week (0-6) according to local time
getUTCDate() Gets the day of the month (1–31) according to universal time
Scripting Language
Form validation is an integral part of the user experience on web pages, allowing
clients to ensure accuracy when users are entering data into forms. By offering this
service, both parties can be sure that all relevant details have been correctly filled out
– a key step in any successful interaction!
Java Script boasts a wide repertoire of helpful In-Built Functions like Number(),
parseFloat(), and parseInt(). These functions come in handy for verifying numbers,
converting objects into numerical values, and analyzing strings – making it easier than
ever to work with data!
Looping is a powerful tool used to execute the same code multiple times, allowing
developers to automate tedious and time-consuming tasks easily. This technique can
be employed with specified or indefinite repetition – giving software engineers more
control over their workflow!.
Control Statements
Prototype-based
The way code is written has been revolutionized with JavaScript’s prototype-based
scripting language, which allows for quicker development cycles and efficient use of
memory. Rather than using classes to determine object types, it makes use of a
prototypal inheritance model – much like an Object factory method.
This pattern can be found throughout JavaScript code, optimizing the speed and
reliability of coding projects.
Java script
JavaScript keyboard events
The JavaScript provided keyboard events for the selection, type, and use
of particular forms of information. The event occurs when the key presses
the key up and the key down as per action.