Categories
JavaScript Answers

How to check if first letter of word is a capital letter with JavaScript?

Sometimes, we want to check if first letter of word is a capital letter with JavaScript.

In this article, we’ll look at how to check if first letter of word is a capital letter with JavaScript.

How to check if first letter of word is a capital letter with JavaScript?

To check if first letter of word is a capital letter with JavaScript, we use a regex.

For instance, we write

const word = "Someword";
console.log(/^[A-Z]/.test(word));

to call /^[A-Z]/.test with word to check if the first letter of word starts with a capital letter.

^ is the symbol for the beginning of the string.

[A-Z] is a pattern that matches upper case letters.

Conclusion

To check if first letter of word is a capital letter with JavaScript, we use a regex.

Categories
JavaScript Answers

How to generate sequence of numbers or characters in JavaScript?

Sometimes, we want to generate sequence of numbers or characters in JavaScript.

In this article, we’ll look at how to generate sequence of numbers or characters in JavaScript.

How to generate sequence of numbers or characters in JavaScript?

To generate sequence of numbers or characters in JavaScript, we call the array fill method.

For instance, we write

const chars = Array(8).fill(1);

to call Array to create an array with 8 empty slots.

Then we call fill with 1 to fill all the empty slots with 1’s.

Conclusion

To generate sequence of numbers or characters in JavaScript, we call the array fill method.

Categories
Angular Answers

How to handle window scroll event in Angular and TypeScript?

Sometimes, we want to handle window scroll event in Angular and TypeScript.

In this article, we’ll look at how to handle window scroll event in Angular and TypeScript.

How to handle window scroll event in Angular and TypeScript?

To handle window scroll event in Angular and TypeScript, we create a custom directive.

For instance, we write

export class WindowScrollDirective {
  ngOnInit() {
    window.addEventListener("scroll", this.scroll, true); 
  }

  ngOnDestroy() {
    window.removeEventListener("scroll", this.scroll, true);
  }

  scroll = (event): void => {
    //...
  };
}

to create the WindowScrollDirective directive class.

In it, we call window.addEventListener to listen to the window’s scroll event when the directive mounts.

scroll is called when we scroll.

When we destroy the directive, we call removeEventListener to remove the scroll listener.

Conclusion

To handle window scroll event in Angular and TypeScript, we create a custom directive.

Categories
JavaScript Answers

How to fix for each … in not working in Node.js and JavaScript?

Sometimes, we want to fix for each … in not working in Node.js and JavaScript.

In this article, we’ll look at how to fix for each … in not working in Node.js and JavaScript.

How to fix for each … in not working in Node.js and JavaScript?

To fix for each … in not working in Node.js and JavaScript, we use the array forEach method.

For instance, we write

array.forEach((item) => {
  //...
});

to call array.forEach with a callback.

And we get the item being looped through from item.

Conclusion

To fix for each … in not working in Node.js and JavaScript, we use the array forEach method.

Categories
JavaScript Answers

How to traverse through JavaScript object properties?

Sometimes, we want to traverse through JavaScript object properties.

In this article, we’ll look at how to traverse through JavaScript object properties.

How to traverse through JavaScript object properties?

To traverse through JavaScript object properties, we use the for-of loop and the Object.entries property.

For instance, we write

const object1 = {
  a: "somestring",
  b: 42,
};

for (const [key, value] of Object.entries(object1)) {
  console.log(`${key}: ${value}`);
}

to call Object.entries to return an array with the key-value pair arrays in object1.

Then we use a for-of loop to get the key and value of each property.

Conclusion

To traverse through JavaScript object properties, we use the for-of loop and the Object.entries property.