Web Tech Unit 2 Part1
Web Tech Unit 2 Part1
1. Introduction to JavaScript
What is JavaScript?
JavaScript is a high-level, interpreted, client-side scripting language primarily used for web
development. It was created by Brendan Eich in 1995 while working at Netscape Communications.
Key Characteristics:
Dynamic Language: Variables don't need explicit type declaration
Interpreted Language: No compilation required, executed by browser's JavaScript engine
Features of JavaScript:
1. Client-Side Validation: Validates user input before sending to server
Advantages:
Reduces server load by performing client-side operations
No compilation required
Supports multiple programming paradigms
Disadvantages:
Code is visible to users (security concern)
2. Objects in JavaScript
Definition:
Objects in JavaScript are collections of key-value pairs where keys are strings (properties) and values
can be any data type including functions (methods).
javascript
let person = {
name: "John",
age: 25,
city: "New York",
greet: function() {
return "Hello";
}
};
2. Object Constructor:
javascript
3. Constructor Function:
javascript
Object Properties:
Property Access:
Dot notation: object.property
Object Methods:
Methods are functions stored as object properties. They can access object properties using the this
keyword.
Built-in Objects:
1. Array: For storing ordered collections
Object Characteristics:
Objects are reference types
2. String:
3. Boolean:
4. Undefined:
5. Null:
6. Symbol (ES6):
Unique identifier
Operations:
Arithmetic Operations:
Subtraction: -
Multiplication: *
Division: /
Modulus: %
Exponentiation: **
Increment: ++
Decrement: --
Comparison Operations:
Not Equal: !=
Logical Operations:
AND: &&
OR: ||
NOT: !
Assignment Operations:
Basic Assignment: =
Addition Assignment: +=
Subtraction Assignment: -=
Multiplication Assignment: *=
Division Assignment: /=
Expressions:
An expression is any valid unit of code that resolves to a value.
Types of Expressions:
1. Arithmetic Expressions: 5 + 3 * 2
2. String Expressions: "Hello" + " World"
4. Assignment Expressions: x = 10
Type Conversion:
Implicit Conversion: Automatic type conversion by JavaScript
4. Control Statements
Definition:
Control statements determine the flow of program execution based on certain conditions or criteria.
1. Conditional Statements:
if Statement:
javascript
if (condition) {
// code to execute if condition is true
}
if...else Statement:
javascript
if (condition) {
// code if condition is true
} else {
// code if condition is false
}
if (condition1) {
// code for condition1
} else if (condition2) {
// code for condition2
} else {
// code if no condition is true
}
switch Statement:
javascript
switch (expression) {
case value1:
// code for value1
break;
case value2:
// code for value2
break;
default:
// default code
}
2. Looping Statements:
for Loop:
javascript
while Loop:
javascript
while (condition) {
// code to repeat
}
do...while Loop:
javascript
do {
// code to repeat
} while (condition);
for...in Loop:
javascript
for...of Loop:
javascript
3. Jump Statements:
break Statement:
continue Statement:
return Statement:
Definition:
Arrays are ordered collections of elements that can store multiple values in a single variable. Each
element has an index starting from 0.
Array Creation:
1. Array Literal:
javascript
2. Array Constructor:
javascript
Array Properties:
Length Property:
Array Methods:
Adding Elements:
Removing Elements:
Iterating Arrays:
Multi-dimensional Arrays:
Arrays can contain other arrays, creating multi-dimensional structures:
javascript
Array Characteristics:
Arrays are dynamic (size can change)
6. Functions
Definition:
Functions are reusable blocks of code designed to perform specific tasks. They are executed when
called (invoked).
Function Declaration:
javascript
function functionName(parameters) {
// function body
return value; // optional
}
Function Expression:
javascript
javascript
Function Components:
1. Function Name:
2. Parameters:
4. Return Statement:
Function Invocation:
javascript
functionName(arguments);
Types of Functions:
1. Named Functions:
2. Anonymous Functions:
javascript
(function() {
// code executed immediately
})();
4. Callback Functions:
Function Scope:
Local Scope: Variables declared inside function
7. Constructors
Definition:
Constructor functions are special functions used to create and initialize objects. They serve as
blueprints for creating multiple objects with similar properties and methods.
javascript
function ConstructorName(parameters) {
this.property1 = value1;
this.property2 = value2;
this.method = function() {
// method body
};
}
javascript
4. Returns the new object (unless constructor explicitly returns something else)
Constructor Example:
javascript
Built-in Constructors:
1. Object Constructor:
javascript
2. Array Constructor:
javascript
3. Function Constructor:
javascript
4. Date Constructor:
javascript
Prototype Property:
Every constructor function has a prototype property that allows adding properties and methods to all
instances:
javascript
Person.prototype.nationality = "Unknown";
Person.prototype.getAge = function() {
return this.age;
};
Advantages of Constructors:
Code reusability
Consistent object structure
Encapsulation of related properties and methods
Constructor Characteristics:
Constructors are regular functions called with new