Javascript Notes
Javascript Notes
Function arguments are the values received by the function when it is invoked.
Inside the function, the arguments (the parameters) behave as local variables.
FUNCTION INVOCATION :
The code inside the function will execute when "something" invokes (calls) the function:
function myFunction(a, b) {
return a * b;
Why Functions?
You can write code that can be used many times.
You can use the same code with different arguments, to produce different
results.
The () operator
The () operator invokes (calls) the function:
function toCelsius(fahrenheit) {
return (5/9) * (fahrenheit-32);
}
Since local variables are only recognized inside their functions, variables with the same name
can be used in different functions.
Local variables are created when a function starts, and deleted when the function is completed.
What is an Object?
4
Object Methods
// Create an Object
const person = {
name: "John",
age: 30,
city: "New York"
};
// Stringify Object
let myString = JSON.stringify(person);
// Display String
document.getElementById("demo").innerHTML = myString;
Object Constructors
Sometimes we need to create many objects of the same type.
Strings in JavaScript have several <p>The length property returns the length of a
built-in properties. string.</p>
12
String Methods
Accessing elements in
multidimensional arrays requires
multiple indices.
Multidimensional Arrays definition ways
1st, need to define some 1D let salary = [ // Remove last element from 4th
array sub-array
["ABC", 24, 18000],
let arr1 = ["ABC", 24, 18000]; // That is 28000 indexing
["EFG", 30, 30000], starts from 0
let arr2 = ["EFG", 30, 30000];
["IJK", 28, 41000], salary[3].pop();
let arr3 = ["IJK", 28, 41000];
["EFG", 31, 28000],
let arr4 = ["EFG", 31, 28000];
]; // Removes last sub-array
let arr5 = ["EFG", 29, 35000];
salary.push(["MNO", 29, // That is "["EFG", 31, 28000]"
// "salary" defines like a 1D 33300]);
array but it already contains salary.pop();
some 1D array // This row added after the
last row in the "salary" array
let salary = [arr1, arr2, arr3,
arr4, arr5];
let salary = [
["ABC", 24, 18000],
after adding "India" at the 4th array : // Adding "India" at the 4th index of 4th sub array
salary[3][3] = "India";
ABC, 24, 18000
IJK, 28, 41000, USA, Canada // Adding "USA" and "Canada" in the 2nd sub-array
for (let i = 0; i < salary.length; i++) {
EFG, 31, 28000, India console.log(salary[i]);
Array Destructuring
if (condition) {
// block of code to be if (condition1) {
executed if the condition is // block of code to be executed if
true condition1 is true
} } else if (condition2) {
// block of code to be executed if the
if (condition) { condition1 is false and condition2 is true
// block of code to be executed if the } else {
condition is true // block of code to be executed if the
} else { condition1 is false and condition2 is false
// block of code to be executed if the }
condition is false
}
switch
● The switch expression is evaluated once.
● The value of the expression is compared with the values of each case.
● If there is a match, the associated block of code is executed.
● If there is no match, the default code block is executed.
switch(expression) { If the break statement is omitted then, the next case will be executed even if the
case x: evaluation does not match the case.
// code block
break; The default keyword specifies the code to run if there is no case match
case y:
// code block
If default is not the last case in the switch block, remember to end the
break;
default case with a break.
default:
// code block
} The default case does not have to be the last case in a switch block
Expression 1 is executed (one time) before the execution of the code block.
Expression 3 is executed (every time) after the code block has been executed.
Do not use for in over an Array if the index
order is important.
javascript loops
The index order is implementation-dependent,
and array values may not be accessed in the
const person = {fname:"John", lname:"Doe", age:25}; order you expect.
let text = ""; It is better to use a for loop, a for of loop, or
for (let x in person) {
Array.forEach() when the order is important.
text += person[x];
}
It lets you loop over iterable data structures such as Arrays, Strings, Maps, NodeLists, and more:
Inside the class, the constructor ● It has to have the exact name
method is defined using the "constructor"
`constructor` keyword. ● It is executed automatically
when a new object is created
● It is used to initialize object
Class methods can be added directly
properties
within the class body without needing
the `function` keyword. If you do not define a constructor
method, JavaScript will add an empty
constructor method.
<script>
class Car {
Class constructor method constructor(name, year) {
this.name = name;
this.year = year;
class ClassName {
constructor() { ... }
method_1() { ... } }
method_2() { ... } age() {
method_3() { ... } const date = new Date();
} return date.getFullYear() - this.year;
}
}
Class methods are created with the
same syntax as object methods.
const myCar = new Car("Ford", 2014);
document.getElementById("demo").inner
Use the keyword class to create a HTML =
class. "My car is " + myCar.age() + " years old.";
</script>
Always add a constructor()
method.
<script>
function myFunction() {
const message = document.getElementById("p01");
message.innerHTML = "";
let x = document.getElementById("demo").value;
try {
if(x.trim() == "") throw "empty";
if(isNaN(x)) throw "not a number";
x = Number(x);
if(x < 5) throw "too low";
if(x > 10) throw "too high";
}
catch(err) {
message.innerHTML = "Input is " + err;
}
}
</script>
</body>
</html>
Debugging Techniques Overview
Debugging is the process of
identifying and resolving
bugs in code to ensure
smooth functionality.
JavaScript provides built-in support for JavaScript can change all the HTML elements
DOM manipulation through the in the page
`document` object. JavaScript can change all the HTML attributes
in the page
Developers can use methods like
JavaScript can change all the CSS styles in the
`getElementById`, `createElement`, page
and `appendChild`.
JavaScript can remove existing HTML elements
and attributes
This allows for real-time updates of
XML data in web applications. JavaScript can add new HTML elements and
attributes
Examples of validation
tools include XMLSpy,
Xerces, and online
validators.
Common XML Applications