bobbyhadz blog

I write articles about Web Development and Cloud Computing.

How to use shorthand for if/else statement in JavaScript

How to use shorthand for if/else statement in JavaScript

To use a shorthand for an if else statement, use the ternary operator. The ternary operator starts with a condition that is followed by a question mark `?`, then a value to return ...

javascript
avatar

Borislav Hadzhiev

Sun Dec 05 20213 min read

Format a Date as YYYY-MM-DD hh:mm:ss in JavaScript

Format a Date as YYYY-MM-DD hh:mm:ss in JavaScript

To format a date as yyyy-mm-dd hh:mm:ss get all the components of the date using the methods on the Date object. Add a leading zero to the day, month, hours, minutes and seconds if...

javascript
avatar

Borislav Hadzhiev

Sat Dec 04 20213 min read

Fix - removeEventListener not working in JavaScript

Fix - removeEventListener not working in JavaScript

To resolve the `removeEventListener` not working issue, make sure to pass the same function to the `addEventListener` and `removeEventListener` methods. Passing a function that poi...

javascript
avatar

Borislav Hadzhiev

Fri Dec 03 20212 min read

Remove the last Comma from a String in JavaScript

Remove the last Comma from a String in JavaScript

To remove the last comma from a string, call the `replace()` method with the following regular expression `/,*$/` as the first parameter and an empty string as the second. The `rep...

javascript
avatar

Borislav Hadzhiev

Thu Dec 02 20214 min read

Array.push() Element if does not exist using JavaScript

Array.push() Element if does not exist using JavaScript

To push an element in an array if it doesn't exist, use the `includes()` method to check if the value exists in the array, and push the element if it's not already present. The `in...

javascript
avatar

Borislav Hadzhiev

Wed Dec 01 20213 min read

Convert a String to a Boolean in JavaScript

Convert a String to a Boolean in JavaScript

To convert a string to a boolean, use the equality operator to compare the string to the string `true`, e.g. `const bool = str === 'true'`. If the condition is met, the strict equa...

javascript
avatar

Borislav Hadzhiev

Tue Nov 30 20213 min read

Join non-Empty Strings with a Separator in JavaScript

Join non-Empty Strings with a Separator in JavaScript

To join non-empty strings with a separator, add the strings to an array and call the `filter()` method on the array to filter only for truthy values, e.g. `[str1, str2].filter(Bool...

javascript
avatar

Borislav Hadzhiev

Mon Nov 29 20212 min read

Solve - Cannot set Property which has only a Getter in JS

Solve - Cannot set Property which has only a Getter in JS

The "Cannot set property which has only a getter" error occurs when trying to set a new value to a property, for which only a getter is defined. To solve the error, define a setter...

javascript
avatar

Borislav Hadzhiev

Mon Nov 29 20212 min read

Check if all Values in Object are Equal in JavaScript

Check if all Values in Object are Equal in JavaScript

To check if all of the values in an object are equal, use the `Object.values()` method to get an array of the object's values and convert the array to a `Set`. If the `Set` contain...

javascript
avatar

Borislav Hadzhiev

Sun Nov 28 20213 min read

Check if a Value is an Object using JavaScript

Check if a Value is an Object using JavaScript

To check if a value is an object check that the value has a type of object - `typeof variable === 'object'`. Then verify the value is not `null` - `variable !== null` and verify th...

javascript
avatar

Borislav Hadzhiev

Sat Nov 27 20212 min read

Iterate over a Set in Reverse Order in JavaScript

Iterate over a Set in Reverse Order in JavaScript

To iterate over a `Set` in reverse order use the `Array.from()` method to convert the `Set` to array. Then use the `reverse()` method to reverse the array and call the `forEach` me...

javascript
avatar

Borislav Hadzhiev

Fri Nov 26 20212 min read

Split a String by Space or Comma using JavaScript

Split a String by Space or Comma using JavaScript

To split a string by space or comma, pass the following regular expression to the `split()` method - `/[, ]+/`. The method will split the string on each occurrence of a space or co...

javascript
avatar

Borislav Hadzhiev

Thu Nov 25 20213 min read

Convert all Values in a Set to Lowercase in JavaScript

Convert all Values in a Set to Lowercase in JavaScript

To convert all values in a `Set` to lowercase, create a second `Set`, that will store the lowercase values. Then use the `forEach` method to iterate over the first `Set` and on eac...

javascript
avatar

Borislav Hadzhiev

Wed Nov 24 20212 min read

Split String into Substrings of N characters in JavaScript

Split String into Substrings of N characters in JavaScript

To split a string into substring on N characters, call the `match()` method on the string, passing it the following regular expression `/.{1, N}g/`. The `match` method will return ...

javascript
avatar

Borislav Hadzhiev

Tue Nov 23 20213 min read

Swap the Keys and Values in an Object using JavaScript

Swap the Keys and Values in an Object using JavaScript

To swap the keys and values in an object, call the `Object.entries()` method to get an array of key-value pairs and use the `map()` method to switch the places of the key and the v...

javascript
avatar

Borislav Hadzhiev

Mon Nov 22 20213 min read

Replace/Remove characters that Don't match Regex in JS

Replace/Remove characters that Don't match Regex in JS

To replace or remove characters that don't match a regex, call the `replace()` method on the string passing it a regular expression that uses the caret `^` symbol, e.g. `/[^a-z]+/`...

javascript
avatar

Borislav Hadzhiev

Sun Nov 21 20212 min read

Solve - map() method returns undefined in JavaScript

Solve - map() method returns undefined in JavaScript

The reason the `map()` method returns undefined values is because we forget to explicitly return a value in the callback function we passed to the method.

javascript
avatar

Borislav Hadzhiev

Sun Nov 21 20212 min read

Sort an Array without Mutation using JavaScript

Sort an Array without Mutation using JavaScript

Use the spread operator (...) to sort an array without mutating the original, e.g. `[...arr].sort()`. The spread operator creates a shallow copy of the original array, on which you...

javascript
avatar

Borislav Hadzhiev

Sun Nov 21 20212 min read

author avatar

Borislav Hadzhiev

Web Developer

SUPPORT ME : )

buy me a coffee
Previous PreviousNextNext
Buy Me A Coffee