How to Force Download Links in HTML

I recently discovered a simple way to force a download of files in HTML by utilizing the download attribute in an <a> tag. By adding the download attribute to a link, you can ensure that the file is downloaded by the browser, even if it could be displayed within the browser itself (such as a PDF file). To implement this, follow these steps: Add an <a> tag with the href attribute pointing to the file you want to force download....

How to Format a Date in JavaScript

Formatting dates in JavaScript is a common task. There are several methods available to generate a string representation of a date object. Let’s explore the different options: Given a Date object: const date = new Date('July 22, 2018 07:22:13'); The built-in methods provide various string representations: date.toString(); // "Sun Jul 22 2018 07:22:13 GMT+0200 (Central European Summer Time)" date.toTimeString(); // "07:22:13 GMT+0200 (Central European Summer Time)" date.toUTCString(); // "Sun, 22 Jul 2018 05:22:13 GMT" date....

How to Format a Number as a Currency Value in JavaScript

In this blog post, we will learn how to convert a number into a currency value using the JavaScript Internationalization API. By the end, you will be able to effortlessly transform numbers into currency formats based on different countries’ conventions. Let’s start with a simple example: suppose we have a number, 10, which represents the price of something. We want to display it as $10.00 in USD. However, if the number has more than 3 digits, the format should be different....

How to Generate a Local SSL Certificate

Note: The commands provided in this blog post were tested on macOS. They should work similarly on Linux, but I cannot guarantee their effectiveness on Windows. To generate a local SSL certificate, follow these steps: Open the terminal and navigate to the root folder of your project. Run the following OpenSSL command to generate a self-signed certificate and private key: openssl req -x509 -newkey rsa:2048 -keyout keytmp.pem -out cert.pem -days 365 This command generates two files: cert....

How to Get a Random Item from an Array in Swift

In this tutorial, we will learn how to retrieve a random item from an array in Swift. Let’s say we have an array like this: let items = [1, 2, 3] and we want to extract a random element from it. In Swift, the Array data type offers a convenient function called randomElement() that returns an optional element (Element?). This function allows us to easily get a random item from an array....

How to Get All Files in a Folder Recursively in Node.js

Do you need to list all the files in a folder in Node.js? If so, keep reading to find out how to do it. There are various ways to achieve this, but one of the best approaches is to use the glob library. Here’s how you can install it: npm install glob Let’s say you want to find all the index.md files within the content/post folder, including those in subfolders. For example:...

How to Get Query String Values in JavaScript with URLSearchParams

Accessing and modifying query string values is a common task when working with web pages. In JavaScript, we can accomplish this using the URLSearchParams API, which is supported by all modern browsers. Query Parameters in a URL A query string is a part of a URL that follows the ? symbol and contains key-value pairs. For example: https://test.com/hello?name=roger&age=20 In this case, we have two parameters: name with a value of roger, and age with a value of 20....

How to Get Started with Freelancing as a Developer

Recently, I received a question on Twitter from someone who wants to venture into freelance work as a developer. They expressed their long-time desire to work for themselves but lacked the courage and motivation to take the leap. They asked for advice and the knowledge I wish I had when I first started freelancing. Let’s dive into this important topic. Freelancing offers the ultimate freedom. As someone who has never been employed by a company, I began my freelancing journey in 2008....

How to Get the Current Timestamp in JavaScript

Discover the different methods JavaScript provides for generating the current UNIX timestamp. The UNIX timestamp is an integer that represents the number of seconds elapsed since January 1, 1970. On UNIX-like machines, such as Linux and macOS, you can easily find the UNIX timestamp by typing date +%s in the terminal: $ date +%s 1524379940 In JavaScript, the current timestamp can be obtained by calling the now() method on the Date object:...

How to Get the File Extension in Node.js

In certain cases, you may come across the need to determine the extension of a file, such as an image file. To accomplish this task effectively in Node.js, you can utilize the path built-in module and its extname() method. Below is an example of how to use this method: const path = require('path'); path.extname('picture.png'); //.png path.extname('picture.of.a.dog.png'); //.png path.extname('picture.of.a.dog.jpg'); //.jpg path.extname('picture.of.a.dog.jpeg'); //.jpeg By employing the path module and its extname() method, you can easily extract the extension from the given file name....