⤳ If you’re looking for Javascript shorthand for if/else statements, you’re probably looking for the ternary – a.k.a the conditional – operator. JavaScript ternary operator takes three operands: a condition followed by a question mark (?), and two JavaScript expressions separated by a colon (:). The expression on ...
⤳ What are those double not operators in JavaScript? You might have noticed the JS double exclamation mark (!!) in a code snippet, and you may be curious what that means. First, “double exclamation mark” (a.k.a the “double bang”) isn’t an operator itself. It’s two logical not ...
⤳ If you need to get the day of the week in JavaScript, you can do so with the standard Date object methods – without using a third-party library. The Date object can give us the day of the week in the following formats: Using Date.prototype.getDay() ...
⤳ To subtract a few days from a date in JavaScript, you need to use setDate() on a Date object and decrement the current day of the month – returned by getDate() – by one or more days. You can do it in three steps: For instance, to get the ...
⤳ To add a few days to a date in JavaScript, you need to use setDate() on the Date object and increment the current day of the month by one or more days. You can do it in three steps: For instance, to add 5 days to a ...
⤳ To get yesterday’s date in JavaScript, you need to get today’s date and use setDate() of the Date object to subtract a day from it. It’s quite easy to implement; You can do it in four steps: Let’s write the code: In the above example, ...
⤳ The error “__dirname is not defined in ES module scope” occurs if you refer to the __dirname global variable in an ES (ECMAScript) module. Here’s what the error message looks like: This global variable contains the path to the current module’s directory. The __dirname and ...
⤳ Sometimes you need to get the viewport width (or height) in JavaScript – probably to adjust an element based on the window size. The good news is, it’s easy and quick! Let’s see how. Wait! What is the viewport size? You may ask. Here’s an ...
⤳ In JavaScript, to check if an element exists or not, you need to access it first. You can use one the following methods to access DOM elements: Whenever you access a web page, the web browser parses the HTML code and populates a DOM tree ...
⤳ If you’re looking for the JavaScript isset equivalent, you’ve probably used it in PHP before and want the same functionality in JavaScript. As you probably know, PHP’s isset() function checks a variable is declared and not null. Although JavaScript doesn’t have an isset equivalent, there are ...