Monthly Archives: August 2022

JavaScript Strings

JavaScript strings are for storing and manipulating text. A JavaScript string is zero or more characters written inside quotes. Example <!DOCTYPE html> <html> <body> <h2>JavaScript Strings</h2> <p id=”demo”></p> <script> let text = “John Doe”;  // String written inside quotes document.getElementById(“demo”).innerHTML = text; </script> </body> </html> Result: JavaScript Strings John Doe You can use single or… Read More »

JavaScript Events

HTML events are “things” that happen to HTML elements. When JavaScript is used in HTML pages, JavaScript can “react” on these events. HTML Events An HTML event can be something the browser does, or something a user does. Here are some examples of HTML events: An HTML web page has finished loading An HTML input field was changed An… Read More »

JavaScript Objects

Real Life Objects, Properties, and Methods In real life, a car is an object. A car has properties like weight and color, and methods like start and stop: Object Properties Methods car.name = Fiat car.model = 500 car.weight = 850kg car.color = white car.start() car.drive() car.brake() car.stop() All cars have the same properties, but the property values differ from car to car. All… Read More »

JavaScript Functions

A JavaScript function is a block of code designed to perform a particular task. A JavaScript function is executed when “something” invokes it (calls it). Example <!DOCTYPE html> <html> <body> <h2>JavaScript Functions</h2>​ <p>This example calls a function which performs a calculation, and returns the result:</p> <p id=”demo”></p> <script> function myFunction(p1, p2) {  return p1 *… Read More »

JavaScript Data Types

JavaScript variables can hold different data types: numbers, strings, objects and more:let length = 16;                               // Numberlet lastName = “Johnson”;                      // Stringlet x = {firstName:”John”, lastName:”Doe”};    // Object The Concept of Data Types In programming, data types is an important concept. To be able to operate on variables, it is important to know something about the type. Without data types, a computer cannot… Read More »

JavaScript Arithmetic

JavaScript Arithmetic Operators Arithmetic operators perform arithmetic on numbers (literals or variables). Operator Description + Addition – Subtraction * Multiplication ** Exponentiation (ES2016) / Division % Modulus (Remainder) ++ Increment — Decrement Arithmetic Operations A typical arithmetic operation operates on two numbers. The two numbers can be literals: Example <!DOCTYPE html> <html> <body>​ <h2>JavaScript Arithmetic</h2>… Read More »

JavaScript Operators

Example Assign values to variables and add them together:<!DOCTYPE html><html><body><h2>JavaScript Operators</h2><p>x = 5, y = 2, calculate z = x + y, and display z:</p><p id=”demo”></p><script>let x = 5;let y = 2;let z = x + y;document.getElementById(“demo”).innerHTML = z;</script></body></html> Result: JavaScript Operators x = 5, y = 2, calculate z = x + y, and… Read More »

JavaScript Const

The const keyword was introduced in ES6 (2015). Variables defined with const cannot be Redeclared. Variables defined with const cannot be Reassigned. Variables defined with const have Block Scope. Cannot be Reassigned A const variable cannot be reassigned: Example <!DOCTYPE html> <html> <body> <h2>JavaScript const</h2> <p id=”demo”></p> <script> try {  const PI = 3.141592653589793;  PI = 3.14; } catch (err) {  document.getElementById(“demo”).innerHTML = err; } </script>… Read More »

JavaScript Let

The let keyword was introduced in ES6 (2015). Variables defined with let cannot be Redeclared. Variables defined with let must be Declared before use. Variables defined with let have Block Scope. Cannot be Redeclared Variables defined with let cannot be redeclared. You cannot accidentally redeclare a variable. With let you can not do this: Example let x = “John Doe”; let x = 0; // SyntaxError: ‘x’ has already been declared With var you can:… Read More »