Categories
JavaScript Answers

How to Calculate a Person’s Age from Someone’s Birthday with JavaScript?

Sometimes, we want to calculate a person’s age from someone’s birthday with JavaScript.

In this article, we’ll look at how to calculate a person’s age from someone’s birthday with JavaScript.

Calculate a Person’s Age from Someone’s Birthday with JavaScript

To calculate a person’s age from someone’s birthday with JavaScript, we can use native JavaScript date methods.

For instance, we can write:

const getAge = (d1, d2 = new Date()) => {
  const diff = d2.getTime() - d1.getTime();
  return Math.floor(diff / (1000 * 60 * 60 * 24 * 365.25));
}

console.log(getAge(new Date(1988, 10, 3)));

We create the getAge function that takes 2 dates d1 and d2 .

Then we subtract the timestamps of them that we got with getTime with:

const diff = d2.getTime() - d1.getTime();

And then we divide diff with 1000 * 60 * 60 * 24 * 365.25 to convert diff to the number of years differences, which is the person’s age.

Therefore, the console log should log 32.

Conclusion

To calculate a person’s age from someone’s birthday with JavaScript, we can use native JavaScript date methods.

Categories
JavaScript Answers

How to Get the Current Time in Nanoseconds Using JavaScript?

Sometimes, we want to get the current time in nanoseconds using JavaScript.

In this article, we’ll look at how to get the current time in nanoseconds using JavaScript.

Get the Current Time in Nanoseconds Using JavaScript

To get the current time in nanoseconds using JavaScript, we can use the window.performance.now method and window.performance.timing.navigationStart properties.

For instance, we can write:

const ms = window.performance.timing.navigationStart + window.performance.now()  
const ns = ms / 0.001  
console.log(ns)

to get the current timestamp in nanoseconds.

window.performance.timing.navigationStart returns the timestamp in milliseconds when we run the code.

And window.performance.now returns the number of nanoseconds since the code has run.

Then to convert the timestamp in milliseconds to nanoseconds, we divide ms by 0.001.

Conclusion

To get the current time in nanoseconds using JavaScript, we can use the window.performance.now method and window.performance.timing.navigationStart properties.

Categories
JavaScript Answers

How to Set a Button’s Value Using JavaScript?

Sometimes, we want to set a button’s value using JavaScript.

In this article, we’ll look at how to set a button’s value using JavaScript.

Set a Button’s Value Using JavaScript

To set a button’s value using JavaScript, we can set the innerHTML property of the button.

For instance, if we have the following HTML:

<form>
  <button name="submit-button">Original<br>Text</button>
</form>

We can write:

const form = document.querySelector('form')
form.elements["submit-button"].innerHTML = 'submit'

to get the form with document.querySelector .

Then we get the button with the name attribute set to submit-button with:

form.elements["submit-button"]

And we set the innerHTML property of it to 'submit' .

Therefore, the button in the form would have ‘submit’ as its text content.

Conclusion

To set a button’s value using JavaScript, we can set the innerHTML property of the button.

Categories
JavaScript Answers

How to Count the Number of Occurrences for Each Character in a JavaScript String?

Sometimes, we want to count the number of occurrences for each character in a JavaScript string.

In this article, we’ll look at how to count the number of occurrences for each character in a JavaScript string.

Count the Number of Occurrences for Each Character in a JavaScript String

To count the number of occurrences for each char in a JavaScript string, we can create an object that keeps track of how many characters are in each string.

Then we can loop through each character of the string with a for-of loop.

For instance, we can write:

const countChar = (str) => {
  const counts = {};
  for (const s of str) {
    if (counts[s]) {
      counts[s]++
    } else {
      counts[s] = 1
    }
  }
  return counts;
}

const str = "I want to count the number of occurences of each char in this string";
console.log(countChar(str))

to count the number of occurrences in each character in str with the countChar function.

Then function loops through each character of string str with the for-of loop.

Next, we create the counts object to keep track of the count of each character.

Then if count[s] exists, we increment it by 1.

Otherwise, we set count[s] to 1.

And then when the loop is done, we return the counts object.

Conclusion

To count the number of occurrences for each char in a JavaScript string, we can create an object that keeps track of how many characters are in each string.

Then we can loop through each character of the string with a for-of loop.

Categories
JavaScript Answers

How to Get the Text Node After an HTML Element with JavaScript?

Sometimes, we want to get the text node after an HTML element with JavaScript.

In this article, we’ll look at how to get the text node after an HTML element with JavaScript.

Get the Text Node After an HTML Element with JavaScript

To get the text node after an HTML element with JavaScript, we can use the nextsubling.nodeValue property to get the text content of the text node after a given element.

For instance, if we have the following HTML:

<input type="checkbox" name='something' value='v1' /> All the world <br />

Then we can get the node value of the element after it by writing:

const text = document.querySelector('input[name="something"]').nextSibling.nodeValue
console.log(text)

We select the input with:

document.querySelector('input[name="something"]')

Then we use the nextSibling.nodeValue property of it to get the text node value next to it, which is 'All the world' .

Conclusion

To get the text node after an HTML element with JavaScript, we can use the nextsubling.nodeValue property to get the text content of the text node after a given element.