Client Side Scripting Chapter 2
Client Side Scripting Chapter 2
Notes
Define Array -:
In JavaScript, an array is a special type of object used to store multiple values in a single
variable. JavaScript arrays can hold elements of different types and can dynamically resize,
meaning you can add or remove elements after the array is created.
Array Declaration -:
Array declaration is the process of defining an array in a programming language. When you
declare an array, you specify its name, and in some languages, its size and data type. The
array declaration sets aside memory to hold the array's elements.
1. Specify the Data Type (if applicable): Determine what type of elements the array
will store, such as integers, strings, or objects. Some languages like JavaScript don't
require you to specify the data type explicitly.
In JavaScript, arrays are dynamic, so you don't need to specify the size or data type. The
declaration is straightforward:
Array Initialization
Defining array elements refers to the process of assigning values to the individual positions
(or slots) in an array. Each position in an array is identified by an index, allowing you to store
and access elements in a structured way.
Key Concepts:
1. Indexing:
o Arrays are indexed collections, meaning each element is identified by an index
number.
o Indexes usually start from 0, meaning the first element is at index 0, the
second at index 1, and so on.
2. Element Assignment:
o You define or assign values to specific positions in the array using the index.
o The syntax typically involves the array name, followed by the index in square
brackets, and then the assignment operator (=).
3. Element Types:
Looping an Array -:
Looping through an array is a common operation in programming that involves iterating over
each element of the array, typically to access, modify, or perform some operation on each
element. Most programming languages provide various types of loops to facilitate this.
1. For Loop:
o A traditional way to loop through an array, especially when you know the
array's length.
o You typically use an index variable to access each element.
2. For-Each Loop:
o A loop designed to iterate over all elements in a collection (like an array)
without using an explicit index.
o Easier to read and write when you don't need the index.
3. While Loop:
o A loop that continues as long as a specified condition is true.
o Less commonly used for simple array iteration but can be useful in certain
cases.
In JavaScript, you can add elements to an array in several ways, depending on where you
want to place the new element.
The push() method adds one or more elements to the end of an array.
fruits.push("Orange", "Mango");
console.log(fruits); // Outputs: ["Apple", "Banana", "Cherry", "Orange", "Mango"]
The unshift() method adds one or more elements to the beginning of an array.
The splice() method can be used to add elements at a specific position in the array.
• The first argument is the index where the new element should be inserted.
• The second argument is 0, indicating that no elements should be removed.
• The third argument (and any following arguments) are the elements to be added.
You can also add an element to the end of an array by assigning it to the index equal to the
current length of the array.
Although not exactly adding a single element, you can create a new array by concatenating
the existing array with another array or single-element array.
In JavaScript, you can sort the elements of an array using the sort() method. By default, the
sort() method sorts the elements of an array as strings in alphabetical and ascending order.
However, you can customize the sorting behavior by providing a comparison function.
When sort() is called without any arguments, it converts the array elements to strings and
sorts them lexicographically (alphabetical order).
Sorting Numbers
Since sort() converts array elements to strings by default, sorting numbers can produce
unexpected results (e.g., 10 will come before 2 because "10" is less than "2"
lexicographically). To sort numbers correctly, you need to provide a comparison function.
Ascending Order:
let numbers = [40, 100, 1, 5, 25, 10];
numbers.sort(function(a, b) {
return a - b;
});
console.log(numbers); // Outputs: [1, 5, 10, 25, 40, 100]
Descending Order:
let numbers = [40, 100, 1, 5, 25, 10];
numbers.sort(function(a, b) {
return b - a;
});
console.log(numbers); // Outputs: [100, 40, 25, 10, 5, 1]
In JavaScript, a function is a block of code designed to perform a specific task. Functions are
one of the fundamental building blocks in JavaScript and are used to encapsulate code that
can be reused, executed, and invoked with different inputs.
Function Declaration
function functionName(parameters) {
// Code to be executed
}
Function Calling
In JavaScript, calling a function involves invoking or executing the function to perform the
task it was designed for. Once a function is defined, you can call it to run its code. Here’s
how you can do it:
Syntax:
Example:
function greet(name) {
console.log("Hello, " + name + "!");
}
greet("Alice"); // Outputs: "Hello, Alice!"
When calling a function, you pass arguments that correspond to the function's parameters.
Example:
function add(a, b) {
return a + b;
}
let sum = add(5, 10);
console.log(sum); // Outputs: 15
An IIFE is a function that runs as soon as it is defined. It’s a common pattern used to create a
local scope.
Syntax:
(function() {
// Code here runs immediately
})();
Example:
(function() {
console.log("This runs immediately!");
When a function has a return value, you can use it directly in expressions or assign it to
variables.
Example:
function multiply(a, b) {
return a * b;
}
let result = multiply(4, 5);
console.log(result); // Outputs: 20
JavaScript functions can have default parameters, which are used if no argument is provided.
Example:
function greet(name = "Guest") {
console.log("Hello, " + name + "!");
}
greet(); // Outputs: "Hello, Guest!"
greet("Bob"); // Outputs: "Hello, Bob!"
When a function is called as a method of an object, the this keyword refers to the object that
owns the method.
Example:
const person = {
name: "John",
greet: function() {
In JavaScript, a string is a sequence of characters used to represent text. Strings are a fundamental
data type and are used to store and manipulate textual data. JavaScript strings are immutable,
meaning once a string is created, it cannot be changed, but you can create new strings based on
existing ones.
String methods
JavaScript provides a rich set of methods for working with strings. These methods allow you
to perform various operations such as searching, manipulating, and transforming strings.
Here’s a comprehensive overview of commonly used string methods in JavaScript:
1. charAt(index)
2. charCodeAt(index)
4. includes(searchValue, position)
Checks if a string contains a specified value, optionally starting the search from a given
position.
5. indexOf(searchValue, fromIndex)
Returns the index of the first occurrence of a specified value, starting the search from a given
index.
6. lastIndexOf(searchValue, fromIndex)
Returns the index of the last occurrence of a specified value, searching backwards from a
given index.
7. slice(start, end)
Extracts a portion of the string between start and end (not including end).
8. substring(start, end)
Extracts a portion of the string between start and end (not including end). Unlike slice(), it
does not accept negative indices.
9. substr(start, length)
10. toLowerCase()
11. toUpperCase()
12. trim()
Replaces all occurrences of a specified value with a new value (introduced in ES2021).
Checks if the string starts with a specified value, optionally starting the search from a given
position.
Checks if the string ends with a specified value, optionally considering a specified length.
Returns a new string with a specified number of copies of the original string.