🎁GET 20% OFF Using Code 👉SCHOOL👈 Until End 2024🎁

Certificate

CERTIFICATE OF COMPLETIONThis is presented to:Edward OjamboOnline: Learning JAVASCRIPTDate: 2024-09-25Edward OjamboAuthorOjamboShop.com

References

EMCAScript Standards https://ecma-international.org/publications-and-standards/standards

EMCA-262 https://ecma-international.org/publications-and-standards/standards/ecma-262

Basic Use Of Exceptions

Exceptions can be thrown and caught. Surround code in a try block. If an exception is thrown and its current function scope has no catch block, the exception will “bubble up” the call stack to the calling function until it finds a matching catch block. All finally blocks it encounters along the way will be executed.

Try

Sample Codelet x = “”;
try{
	if(x.trim() == “”) throw “empty”;
} catch (err) {
	console.log(“Undefined “+err);
} finally {
	console.log(“Will still print this”);
}
// Type Some Code

Chapter 9: Exceptions

During the execution of a program, it may be required to process special conditions called exceptions. An exception breaks the normal execution flow and executes a specified exception handler.

An exceptional event could occur from an error and the JavaScript will try to find a matching exception block. JavaScript uses try with at least one corresponding catch or finally block. An exception can be thrown or caught.

Table : Exceptions

FunctionDescriptionExample
tryExecutes only if exception triggerstry{ my_function(x); } catch (err) { console.log(“Undefined”); }
catchBlock that handles errortry{ my_function(x); } catch (err) { console.log(“Undefined”); }
throwCustom error messagetry{ if(x.trim() == “”) throw “empty”; } catch (err) { console.log(“Undefined “+err); }
finallyExecutes code regardless of try except block resulttry: print(x) except: print(“Something went wrong”) finally: print(“The ‘try except’ is finished”) if(x.trim() == “”) throw “empty”; } catch (err) { console.log(“Undefined “+err); } finally { console.log(“Will still print this”); }

Class Declaration

An object is created by an executed constructor function of the class. The class defines how objects behave, providing initial values as member variables and implementations of behavior via member functions. Use public and private modifiers to define if functions can be used outside the object.

Class

Sample Codeclass Student {
	constructor(name, sid) {
		this.setName(name);
		this.sid = sid;
	}
	// Getter
	getName() {
		return this.name;
	}
	// Setter
	setName(newName) {
		this.name = newName;
	}
	// Method
	message() {
		return “My name is ” + this.name + ” and my ID is ” + this.sid;
	}
}
const student = new Student(“John Doe”, 123);
console.log(student.message());
console.log(student.name);
// Type Some Code

Object Declaration

An object literal is a list of name value pairs inside curly braces. An object can be created using the new keyword. Objects are containers for properties and methods. Properties are named values. Methods are functions stored as properties.

Object

Sample Codeconst person = {
	name: “John Doe”,
	age: 24,
	greeting : function() {
		return “My name is ” +this.name + ” and I am ” + this.age + ” years old”;
	}
}
console.log(person.greeting());
// Type Some Code

Chapter 8: Objects

An object is a compound data type. Values of more than one type can be stored together in a single variable. An object is an instance of either a built-in or user defined class. An object is initialized by creating a new statement to instantiate a class.

Multiple objects can be created from a class. Each object has all the properties and methods defined in a class but will have different property values.

Table : Common Functions To Manipulate Objects

FunctionDescriptionExample
Object.values()Creates an array from valuesmyArr = Object.values(person);
Object.entries()Simplify object use in loops[key, val] of Object.entries(people)
JSON.stringify()Convert to JSON stringlet myjson = JSON.stringify(person);

Function Declaration

A function is a unit of code that represents a sequence of statements. Functions can accept arguments or values and return a single value (or not). In JavaScript, a function is defined using the function keyword.

Function

Sample Codefunction my_function() {
	console.log(“Hello from my function”);
}
my_function(); // Execute the function
function my_name_function(first_name) {
	let combined = `My first name is ${first_name}`
	return combined;
}
let greeting = my_name_function(“John”);
console.log(greeting);
// Type Some Code

Chapter 7: Functions

A function is a subprogram that can be called by code externally or internally to the function. It is composed of a sequence of statements called the function body. Values can be passed to a function as parameters or arguments and the function can return a value if applicable.

JavaScript comes with built-in functions that can be called directly to perform a specific task. It is also possible to create custom functions also called user defined functions.

A function is therefore a block of statements that can be used repeatedly in a program. It will not execute automatically and must be executed by a call to the function.

Do while Loop


Checks if condition is true after executing a block of code in the loop. The block of code inside the loop will be executed at least once.

Do While Loop

Sample Codelet counter = 0;
do {
	counter += 1;
	console.log(“Executing – counter is ” + counter +”.\n”);
} while (counter < 10);
let even_numbers = [2,4,6,8,10];
counter = 0;
do {
	console.log(even_numbers[counter] + “\n”);
	counter +=1;
} while (counter < even_numbers.length);
// Type Some Code