Monthly Archives: August 2022

JavaScript Modules

Modules JavaScript modules allow you to break up your code into separate files. This makes it easier to maintain the code-base. JavaScript modules rely on the import and export statements. Export You can export a function or variable from any file. Let us create a file named person.js, and fill it with the things we want to export. There are… Read More »

JavaScript Classes

ECMAScript 2015, also known as ES6, introduced JavaScript Classes. JavaScript Classes are templates for JavaScript Objects. JavaScript Class Syntax Use the keyword class to create a class. Always add a method named constructor(): Syntax class ClassName {  constructor() { … }} Example class Car {  constructor(name, year) {    this.name = name;    this.year = year;  }} The example above creates a class named “Car”. The class… Read More »

JavaScript Arrow Function

Arrow functions were introduced in ES6. Arrow functions allow us to write shorter function syntax: Example: <!DOCTYPE html> <html> <body> <h2>JavaScript Arrow Function</h2> <p>This example shows the syntax of an Arrow Function, and how to use it.</p> <p id=”demo”></p> <script> let myFunction = (a, b) => a * b; document.getElementById(“demo”).innerHTML = myFunction(4, 5); </script> </body>… Read More »

The JavaScript this Keyword

Example: <!DOCTYPE html> <html> <body> <h1>The JavaScript <i>this</i> Keyword</h1> <p>In this example, <b>this</b> refers to the <b>person</b> object.</p> <p>Because <b>fullName</b> is a method of the person object.</p> <p id=”demo”></p> <script> // Create an object: const person = {  firstName: “John”,  lastName: “Doe”,  id: 5566,  fullName : function() {    return this.firstName + ” ” +… Read More »

JavaScript Use Strict

“use strict”; Defines that JavaScript code should be executed in “strict mode”. The “use strict” Directive The “use strict” directive was new in ECMAScript version 5. It is not a statement, but a literal expression, ignored by earlier versions of JavaScript. The purpose of “use strict” is to indicate that the code should be executed in “strict mode”. With strict… Read More »

JavaScript Hoisting

Hoisting is JavaScript’s default behavior of moving declarations to the top. JavaScript Declarations are Hoisted In JavaScript, a variable can be declared after it has been used. In other words; a variable can be used before it has been declared. Example 1 gives the same result as Example 2: Example 1: <!DOCTYPE html> <html> <body> <p id=”demo”></p>… Read More »

JavaScript Scope

Scope determines the accessibility (visibility) of variables. JavaScript has 3 types of scope: Block scope Function scope Global scope Block Scope Before ES6 (2015), JavaScript had only Global Scope and Function Scope. ES6 introduced two important new JavaScript keywords: let and const. These two keywords provide Block Scope in JavaScript. Variables declared inside a { } block cannot be accessed from outside the… Read More »

JavaScript Errors

Throw, and Try…Catch…Finally The try statement defines a code block to run (to try). The catch statement defines a code block to handle any error. The finally statement defines a code block to run regardless of the result. The throw statement defines a custom error. Errors Will Happen! When executing JavaScript code, different errors can occur. Errors can be coding errors made by… Read More »

JavaScript Regular Expressions

A regular expression is a sequence of characters that forms a search pattern. The search pattern can be used for text search and text replace operations. What Is a Regular Expression? A regular expression is a sequence of characters that forms a search pattern. When you search for data in a text, you can use this… Read More »

JavaScript Bitwise Operations

JavaScript Bitwise Operators Operator Name Description & AND Sets each bit to 1 if both bits are 1 | OR Sets each bit to 1 if one of two bits is 1 ^ XOR Sets each bit to 1 if only one of two bits is 1 ~ NOT Inverts all the bits << Zero… Read More »