Client Side Scripting Chapter 1
Client Side Scripting Chapter 1
Notes
7. Integrated with HTML/CSS: JavaScript works seamlessly with HTML and CSS to
create dynamic and interactive web pages. It can manipulate the DOM (Document Object
Model) to update content and styles dynamically.
10. JSON Support: JavaScript Object Notation (JSON) is a lightweight data interchange
format that is easy to parse and generate in JavaScript, making it ideal for API integration and
data exchange.
11. Community and Support: JavaScript has a large and active community, providing
extensive resources, tutorials, and support for developers of all levels.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript Example</title>
</head>
<body>
<h1>JavaScript Inline</h1>
<script>
console.log("Hello, world!");
</script>
</body>
</html>
Variables
In JavaScript, variables are used to store data values. You can think of a variable as a
container that holds information that can be referenced and manipulated in a program.
There are three main ways to declare variables in JavaScript: using `var`, `let`, and `const`.
1. `var` Keyword
- Scope: `var` is function-scoped. This means if you declare a variable inside a function using
`var`, it will only be accessible within that function. However, if declared outside any
function, it becomes globally scoped.
var x = 10;
if (true) {
console.log(x); // 20
console.log(x); // 20
2. `let` Keyword
- Scope: `let` is block-scoped. A block is defined by `{}` (curly braces), such as in loops,
conditionals, or functions.
- Hoisting: Variables declared with `let` are also hoisted, but not initialized. This means they
cannot be accessed before their declaration line.
- Re-declaration: You cannot re-declare a variable with `let` in the same scope.
let y = 10;
if (true) {
console.log(y); // 20
console.log(y); // 10
3.`const` Keyword
- Hoisting: Variables declared with `const` are hoisted but not initialized, just like `let`.
- Re-declaration and Assignment: `const` is used to declare constants. A `const` variable must
be initialized at the time of declaration and cannot be re-assigned.
const z = 10;
if (true) {
console.log(z); // 20
console.log(z); // 10
In JavaScript, keywords are reserved words that have special meanings and are used to perform
specific tasks. They cannot be used as identifiers (variable names, function names, etc.). Here are
some of the most commonly used JavaScript keywords along with their definitions and uses
1. Arithmetic Operators
2. Comparison (Relational) Operators
3. Bitwise Operators
4. Logical Operators
5. Assignment Operators
6. Special Operators
Comparison operators are mainly used to perform the logical operations that
determine the equality or difference between the values.
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
if(expression){
//content to be evaluated
}
if(expression){
//content to be evaluated if condition is true
}
else{
//content to be evaluated if condition is false
}
It evaluates the content only if expression is true from several expressions. The
signature of JavaScript if else if statement is given below.
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.
default:
code to be executed if above values are not matched;
}
JavaScript Loops
The JavaScript loops are used to iterate the piece of code using for, while, do
while or for-in loops. It makes the code compact. It is mostly used in array.
1. for loop
2. while loop
3. do-while loop
4. for-in loop
The JavaScript for loop iterates the elements for the fixed number of times. It
should be used if number of iteration is known. The syntax of for loop is given
below.
<script>
for (i=1; i<=5; i++)
{
document.write(i + "<br/>")
}
</script>
The JavaScript while loop iterates the elements for the infinite number of
times. It should be used if number of iteration is not known. The syntax of while
loop is given below.
while (condition)
{
code to be executed
}
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.
do{
code to be executed
}while (condition);
<script>
var i=21;
do{
document.write(i + "<br/>");
i++;
}while (i<=25);
</script>
Alert box
An alert box is often used if you want to make sure information comes through
to the user.
When an alert box pops up, the user will have to click "OK" to proceed.
Syntax
window.alert("sometext");
Confirm Box
A confirm box is often used if you want the user to verify or accept something.
When a confirm box pops up, the user will have to click either "OK" or
"Cancel" to proceed.
If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box
returns false.
Syntax
window.confirm("sometext");
Prompt Box
A prompt box is often used if you want the user to input a value before entering
a page.
When a prompt box pops up, the user will have to click either "OK" or "Cancel"
to proceed after entering an input value.
If the user clicks "OK" the box returns the input value. If the user clicks
"Cancel" the box returns null.
Syntax
window.prompt("sometext","defaultText");