Home » Programming » Javascript

Use appendChild() in JavaScript to Add Page Elements [Examples]

JavaScript appendChild()

The Node.appendChild() method adds a new HTML Element to the page (via the DOM). This article will show you how to use it. The DOM in JavaScript The HTML DOM (Document Object Model) is the interface which JavaScript uses to read from and interact with HTML page elements. It’s a data structure which represents every HTML tag present on the page – everything from the root <html> element that wraps your page to every <div>, <p>, <input>, and <em> – everything on the page is represented in the DOM, both content and structure. Each … Read more

Home » Programming » Javascript

Generate a Hash from String in Javascript, with Examples

JavaScript Hash String

This tutorial will show you a practical way to generate a hash for a string in JavaScript, and provide working code examples you can use in your own project. What is a Hash? A Hash or Checksum is a calculated value with a specific length. By passing a hashing algorithm a string – which can be any length – a calculated hash of a consistent length is returned. Whenever the same string is passed to the hashing algorithm, the same calculated hash should be returned. … Read more

Home » Programming » Javascript

JavaScript setAttribute() Method-What It Is/How to Use It [Examples]

JavaScript setAttribute()

JavaScript was built for adding interactivity to web pages. It can interact with and modify on-screen elements, including their HTML attributes – thats what setAttribute() does. Here’s how to use it. What is the HTML DOM? The HTML DOM (Document Object Model) is the interface which JavaScript uses to read from and interact with HTML page elements. It’s a data structure which represents every HTML tag present on the page – everything from the root <html> element that wraps your page to every <div>, <p>, <input>, and <em> – everything on the page is … Read more

Home » Programming » Javascript

Hash Tables/Associative Arrays in JavaScript – How and Why?

JavaScript Hash Tables

JavaScript does not have a native object class called a “hash table” – but that doesn’t mean that the functionality of hash tables doesn’t exist in the language. Here’s how to use hash tables in JavaScript, with working example code. What is a Hash Table? Also known as hash maps, hash tables are a data structure which maps a list of keys to a list of corresponding values for each key. Any value in the table can be retrieved by accessing it through the associated key. … Read more

Home » Programming » Javascript

Converting String to Boolean in JavaScript, With Examples

JavaScript string to boolean

This tutorial will show you how to convert a string value to boolean in JavaScript, with working code examples for several situations. Why Are You Converting Strings to Booleans? Really, you should not be storing boolean data in a string, but there are several scenarios where it can arise: Boolean values arising from user input Values taken from HTML form elements, which can only contain string values Data taken from poorly formatted third party sources, like APIs or CSV files Once you have data containing … Read more

Home » Programming » Javascript

Using the JavaScript toUpperCase() String Method [Examples]

JavaScript toUpperCase()

This article shows how the toUpperCase() method is used in JavaScript to make all letters in a string UPPER CASE. The toUpperCase() method is included in the string object and is fully supported in all browsers. In the JavaScript programming language, strings can either be a primitive or an object – primitives are the most basic kinds of variables – they have no methods, and simply represent the raw data for the given characters. String objects, however, have additional methods for manipulating and measuring the string – the toUpperCase() is one of these helpful methods. String variables in … Read more

Home » Programming » Javascript

How to Create and Use JavaScript Classes [Examples]

JavaScript Class

This tutorial will explain JavaScript classes and how they relate to objects. Syntax and code examples are provided. But first, a quick refresher on JavaScript objects. JavaScript Objects In JavaScript, objects are a container for a set of properties and methods. A car, for example, could be modelled as an object. It could have properties describing the manufacturer, year it was made, and the colour. Methods are functions which are part of the object, which when called, interact with the object in some way. A car might have a honk method which triggers an alert, … Read more

Home » Programming » Javascript

How to Check if a Variable is a String in JavaScript

JavaScript Check if String

This quick tip will show you how to check if a variable is a string in the JavaScript Programming Language. What is a String? A string is a type of variable. A variable type determines what values a variable can contain and what can be done with the variable. Strings are a series of characters – letters or numbers or symbols. They can be joined, split, and iterated over. Strings are used to store words, sentences, and other non-numeric data like encoded images or serialized data which is going … Read more

Home » Programming » Javascript

JavaScript Functions Tutorial, With Examples

JavaScript Functions

Functions are reusable bits of code which are encapsulated so that you can easily call them by name when you need them. Here’s how they work in JavaScript, with examples. What are Functions? When programming, you will need to perform the same set of actions multiple times on different data. For example, you may need to perform a calculation on all of the rows in a table, or update the values in a list of objects. It is not wise, or practical, to re-write the same … Read more

Home » Programming » Javascript

Current Time in Another Location/Timezone [JavaScript]

JavaScript conver time to another timezone

This article will demonstrate how to get the current time in another location/timezone in Javascript, with just one line of code. The Simplest Pure-JavaScript Way The below function converts a given date to a different timezone, and contains just 1 line of code. function timezoneConvert(date, tzString) { return new Date((typeof date === “string” ? new Date(date) : date).toLocaleString(“en-US”, {timeZone: tzString})); } This function expects: A date, as either a Date object or string A string containing the name of the timezone to convert to This … Read more