Categories
JavaScript Answers

How to Get Computed Styles of an Element with JavaScript?

Sometimes, we want to get computed styles of an element with JavaScript.

In this article, we’ll look at how to get computed styles of an element with JavaScript.

Get Computed Styles of an Element with JavaScript

To get computed styles of an element with JavaScript, we can use the window.getComputedStyle method.

For instance, if we have:

<div>
  hello
</div>

Then we write:

const element = document.querySelector('div')
const style = window.getComputedStyle(element)
console.log(style)

We get the div with document.querySelector.

Then we call window.getComputedStyle with element as the argument.

Therefore, the console log should show all the computed CSS properties with their values for the div.

Conclusion

To get computed styles of an element with JavaScript, we can use the window.getComputedStyle method.

Categories
JavaScript Answers

How to Match a Whole Word in JavaScript?

Sometimes, we want to match a whole word in JavaScript.

In this article, we’ll look at how to match a whole word in JavaScript.

Match a Whole Word in JavaScript

To match a whole word in JavaScript, we can create a regex with the RegExp constructor.

Then we can call the test method on it to check if the string matches the words.

For instance, we can write:

const lookup = '\n\n\n\n\n\nPC Games\n\n\n\n';
const word  = lookup.trim() ;
const match = new RegExp(`\\b${word}\\b`).test('2 PC Games')
console.log(match)

We create the word string with the words we’re looking for.

Then we create the regex with the RegExp constructor.

'\\b' is the word boundary for the pattern. This will let us match the whole word string.

Then we call test on the regex with the string we want to check if it matches the pattern.

Therefore, match should log true since the string matches the pattern.

Conclusion

To match a whole word in JavaScript, we can create a regex with the RegExp constructor.

Then we can call the test method on it to check if the string matches the words.

Categories
JavaScript Answers

How to Loop Through a Date Range with JavaScript?

Sometimes, we want to loop through a date range with JavaScript.

In this article, we’ll look at how to loop through a date range with JavaScript.

Loop Through a Date Range with JavaScript

To loop through a date range with JavaScript, we can use a while loop.

For instance, we can write:

const start = new Date("02/05/2020");  
const end = new Date("02/10/2020");  
let loop = new Date(start);  
while (loop <= end) {  
  console.log(loop);  
  const newDate = loop.setDate(loop.getDate() + 1);  
  loop = new Date(newDate);  
}

We create the start and end date objects that are the start and end dates of the date range.

loop is the variable with the start date.

We increment loop in the while loop until it’s bigger than or equal to end .

To do that, we write:

const newDate = loop.setDate(loop.getDate() + 1);  
loop = new Date(newDate);

Therefore, we see the dates in the date range being looped through and logged to the console.

Conclusion

To loop through a date range with JavaScript, we can use a while loop.

Categories
JavaScript Answers

How to Extract Elements with an Even Index from an Array with JavaScript?

Sometimes, we want to extract elements with an even index from an array with JavaScript.

In this article, we’ll look at how to extract elements with an even index from an array with JavaScript.

Extract Even Elements of an Array with JavaScript

To extract elements with an even index from an array with JavaScript, we can use the array filter instance method.

For instance, we can write:

const arr = [4, 5, 7, 8, 14, 45, 76];
const filtered = arr.filter((element, index) => {
  return (index % 2 === 0);
});
console.log(arr)

to call filter with a callback that has index as the 2nd parameter.

We return index % 2 === 0 to filter out all the elements that have odd indexes.

Therefore, filtered is [4, 5, 7, 8, 14, 45, 76] .

Conclusion

To extract elements with an even index from an array., we can use the array filter instance method.

Categories
JavaScript Answers

How to Change the First Letter of Each Word to Upper Case with JavaScript?

Sometimes, we want to change the first letter of each word to upper case with JavaScript.

In this article, we’ll look at how to change the first letter of each word to upper case with JavaScript.

Change the First Letter of Each Word to Upper Case with JavaScript

To change the first letter of each word to upper case with JavaScript, we can use the JavaScript string’s replace method with a callback to change the first letter of each word to uppercase.

For instance, we can write:

const str = "hello world";
const newStr = str.toLowerCase().replace(/b[a-z]/g, (letter) => {
  return letter.toUpperCase();
});
console.log(newStr)

We call str.toLowerCase to convert str to lower case.

Then we call replace on it with a regex to match all lowercase letters that are the first letter of a word with /b[a-z]/g .

The callback gets the matched letter and call toUpperCase on it.

Therefore, newStr is 'Hello World' .

Conclusion

To change the first letter of each word to upper case with JavaScript, we can use the JavaScript string’s replace method with a callback to change the first letter of each word to uppercase.