How to check if a variable is a string in JavaScript

To check that a JS variable is of a type string, you can use 2 operators: instanceof and typeof. Here鈥檚 a quick solution. if (typeof text === 'string' || text instanceof String) { // it's a string!...

January 27, 2022 路 1 min 路 Coderslang Master

Get random number in a range using JavaScript

To get a random number is JS you almost always rely on the function Math.random. The only issue is that it returns a number between 0 and 1. You can generate a number in a specific range by writing a straightforward function....

January 26, 2022 路 1 min 路 Coderslang Master

How to get screen width and height with JavaScript

The object window.screen holds all the information you about the current screen. You can access the resolution of the screen by accessing its width and height properties. ...

January 25, 2022 路 1 min 路 Coderslang Master

How to remove the last n characters from a JavaScript string

Before you make another step, remember, that strings in JavaScript are immutable. This means that you can鈥檛 alter the string by removing something from it. So, when you want to remove the last n symbols from a string in JS, your only option is to create a new string that wouldn鈥檛 have these n characters....

January 24, 2022 路 2 min 路 Coderslang Master

How to generate a random string in JavaScript

To generate a random string in JS you should start by defining the set of characters that will be used to generate that random string. Let鈥檚 go! ...

January 23, 2022 路 2 min 路 Coderslang Master

Set default parameter values for JavaScript functions

You can add different default values to your JavaScript functions with a simple trick. Let鈥檚 see how it works. ...

January 22, 2022 路 1 min 路 Coderslang Master

How to encode a URL in JavaScript

You can use built-in JavaScript functions encodeURIComponent(str), encodeURI(str) or escape(str) to encode your URLs. Here鈥檚 how you can use it. const urlParam = 'https://google.com'; const encodedURL = "https://coderslang.com?ref=" + encodeURIComponent(urlParam); console....

January 21, 2022 路 1 min 路 Coderslang Master

How to get current date in JavaScript

There are 2 ways you can get current date in JavaScript. You can just create a new Date object without any arguments, or you can use the function Date.now. ...

January 20, 2022 路 1 min 路 Coderslang Master

How to access command-line arguments in Node.js

In Node.js the command-line arguments are captured in the object process.argv. Let鈥檚 start by logging them to the screen. console.log(process.argv); If you run your script without any arguments, the output will be something like this....

January 19, 2022 路 1 min 路 Coderslang Master

How to append something to a JavaScript array

You can append new values to a JS array using the function Array.push. It adds new values to the end of the JavaScript array. ...

January 18, 2022 路 1 min 路 Coderslang Master