Home » Programming

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

How to Generate the Fibonacci Sequence of Numbers in Python

Python fibonacci sequence

This quick tutorial will show you how to generate the Fibonacci sequence of numbers in Python. This is a useful example for new coders and those just learning Python as it shows how variables, loops, and arithmetic are used in Python. What is the Fibonacci Sequence The Fibonacci sequence is a procession of numbers in which each number is the sum of the preceding two numbers. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144… Above, the first 13 numbers in the Fibonacci … Read more

Home » Programming

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

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

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

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 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

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

Home » Programming

The endsWith() Method for JavaScript Strings, with Examples

JavaScript endswith()

The endsWith() String method in JavaScript will determine whether a string ends with a certain string or character. Here’s how to use it, with examples. endsWith() Method JavaScript Syntax The endsWith() method can be called from any String variable. string.endsWith(SEARCH, LENGTH) Note that: string can be any string value or variable SEARCH should be the string which you want to check if string ends with LENGTH is OPTIONAL If specified, LENGTH will be used as the length of the STRING being checked If it is less than the actual length of string, the rest of the string beyond LENGTH will be ignored This can be … Read more

Home » Programming

How to Create a Countdown Timer in JavaScript

JavaScript Countdown Timer

This tutorial will demonstrate how to create a countdown timer in JavaScript, including a function you can copy and paste into your own code. The below code will break down creating a function which calculates the time until a certain future time, and how to run it repeatedly to affect a count down timer. Getting the time remaining until a certain date/time The below function will calculate the days, hours, minutes, and seconds to a target date: function timeToTarget(countdownString){ // Convert the string which specifies … Read more