Home » 2022 » May

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 » 2022 » May

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

Home » 2022 » May

Find/Replace Text in JavaScript with replace() [Examples]

JavaScript find/replace text

This quick tutorial will show you how to find and replace text in JavaScript, with code examples. Replacing text in strings is something you will probably need to do quite often. JavaScript comes with the replace() method as part of it’s String objects to handle this functionality. JavaScript Strings JavaScript Strings are a type of variable used to store and manipulate a sequence of characters. There are also string primitives which represent only the characters and do not contain methods for manipulation, but JavaScript will implicitly convert between the two when required. The … Read more

Home » 2022 » May

The startsWith() Method for JavaScript Strings [Examples]

JavaScript startswith()

The startsWith() String method in JavaScript will determine whether a string starts with a certain string or character. Here’s how it is used, with code examples. startsWith() Method JavaScript Syntax The startsWith() method can be called from any String variable. string.startsWith(SEARCH, START) Note that: string can be any string value or variable SEARCH should be the string which you want to check if string starts with START is OPTIONAL If specified, START will be used as the starting position when checking STRING The portion of the string before START will be ignored This can be useful if you want to check whether a … Read more

Home » 2022 » May

How to Get the Current Date and Time in JavaScript

JavaScript get current date/time

This article will show you how to get the current date, time, or both, in the JavaScript programming language. Code examples included. Date Objects in JavaScript Date objects represent a single point in time in JavaScript. They are created by initializing a new variable with a new Date object: var now = new Date(); By default, new Date objects are initialized to contain the date and time at the instant they were created, along with timezone details for the current device – thus representing the current … Read more

Home » 2022 » May

Promises in JavaScript: What they Are, How to Use Them

JavaScript Promises

Promises are a feature of JavaScript which allow code to continue executing in the background, and perform an action with the result when it has completed. This article will show you how to use Promises. What is a ‘Promise’ in JavaScript? A Promise is an object in JavaScript which acts as a placeholder for the eventual result of an operation. A Promise is either: Pending while the operation completes Fulfilled when the operation is successful Rejected if the operation fails Promises allow for asynchronous code execution in JavaScript. … Read more