Category Archives: Java Script

JavaScript Array Methods

Converting Arrays to Strings The JavaScript method toString() converts an array to a string of (comma separated) array values. Example <!DOCTYPE html> <html> <body> <h2>JavaScript Array Methods</h2> <h2>toString()</h2> <p>The toString() method returns an array as a comma separated string:</p> <p id=”demo”></p> <script> const fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; document.getElementById(“demo”).innerHTML = fruits.toString(); </script> </body> </html> Result: JavaScript… Read More »

JavaScript Arrays

An array is a special variable, which can hold more than one value: Why Use Arrays? If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:let car1 = “Saab”;let car2 = “Volvo”;let car3 = “BMW”; However, what if you want to loop through the cars and find… Read More »

JavaScript Number Methods

Number methods help you work with numbers. Number Methods and Properties Primitive values (like 3.14 or 2014), cannot have properties and methods (because they are not objects). But with JavaScript, methods and properties are also available to primitive values, because JavaScript treats primitive values as objects when executing methods and properties. The toString() Method The toString() method… Read More »

JavaScript Numbers

JavaScript has only one type of number. Numbers can be written with or without decimals. Example <!DOCTYPE html> <html> <body> <h2>JavaScript Numbers</h2> <p>Numbers can be written with or without decimals:</p> <p id=”demo”></p> <script> let x = 3.14; let y = 3; document.getElementById(“demo”).innerHTML = x + “<br>” + y; </script> </body> </html> Result: JavaScript Numbers Numbers… Read More »

JavaScript Template Literals

Synonyms: Template Literals Template Strings String Templates Back-Tics Syntax Back-Tics Syntax Template Literals use back-ticks (“) rather than the quotes (“”) to define a string: Example <!DOCTYPE html> <html> <body> <h2>JavaScript Template Literals</h2> <p>Template literals use back-ticks (“) to define a string:</p> <p id=”demo”></p> <p>Template literals are not supported in Internet Explorer.</p> <script> let text =… Read More »

JavaScript String Search

JavaScript Search Methods String indexOf() String lastIndexOf() String startsWith() String endsWith() JavaScript String indexOf() The indexOf() method returns the index of (the position of) the first occurrence of a specified text in a string: Example <!DOCTYPE html> <html> <body> <h2>JavaScript String Methods</h2> <p>The indexOf() method returns the position of the first occurrence of a specified text:</p> <p id=”demo”></p> <script>… Read More »

JavaScript String Methods

String methods help you to work with strings. String Methods and Properties Primitive values, like “John Doe”, cannot have properties or methods (because they are not objects). But with JavaScript, methods and properties are also available to primitive values, because JavaScript treats primitive values as objects when executing methods and properties. JavaScript String Length The length property… Read More »

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 »