bobbyhadz blog

I write articles about Web Development and Cloud Computing.

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

Prevent adding Duplicates to an Array in JavaScript

Prevent adding Duplicates to an Array in JavaScript

To prevent adding duplicates to an array, call the `includes()` method on the array to check if the value is already present, e.g. `if (!arr.includes(str)) {arr.push(str)}`. The `i...

javascript
avatar

Borislav Hadzhiev

Sat Nov 20 20212 min read

Wait for a Promise to Resolve before Returning in JS

Wait for a Promise to Resolve before Returning in JS

You can use the `async/await` syntax or call the `.then()` method on a promise to wait for it to resolve. Inside of functions marked with the `async` keyword, you can use `await` t...

javascript
avatar

Borislav Hadzhiev

Sat Nov 20 20212 min read

How to Update a Const Array in JavaScript

How to Update a Const Array in JavaScript

Use bracket notation to update the values of an array that was declared using the `const` keyword, e.g. `arr[0] = 'new value'`. The elements of an array declared using `const` can ...

javascript
avatar

Borislav Hadzhiev

Sat Nov 20 20211 min read

author avatar

Borislav Hadzhiev

Web Developer

SUPPORT ME : )

buy me a coffee
Previous PreviousNextNext
Buy Me A Coffee