Tari Ibaba

Tari Ibaba is a software developer with years of experience building websites and apps. He has written extensively on a wide range of programming topics and has created dozens of apps and open-source libraries.

How to Capitalize the First Letter of Each Word in React

To capitalize the first letter of each word in a string in React:

  1. Split the string into an array of words with .split('').
  2. Iterate over the words array with .map().
  3. For each word, return a new word that is an uppercase form of the word’s first letter added to the rest of the word, i.e., word.charAt(0).toUpperCase() + word.slice(1).
  4. Join the words array into a string with .join(' ').

For example:

App.js
export default function App() { const capitalizeWords = (str) => { return str .toLowerCase() .split(' ') .map((word) => word.charAt(0).toUpperCase() + word.slice(1)) .join(' '); }; const str1 = 'coding BEAUTY'; const str2 = 'LEARNING javascript'; return ( <div> <b>{str1}</b> <br /> Capitalized: <b>{capitalizeWords(str1)}</b> <br /> <br /> <b>{str2}</b> <br /> Capitalized: <b>{capitalizeWords(str2)}</b> </div> ); }
Strings, along with the result of capitalizing the first letter of each of their words.
Strings, along with the result of capitalizing the first letter of each of their words.

The capitalizedWords() function takes a string and returns a new string with all the words capitalized.

First, we use the toLowerCase() method to lowercase the entire string, ensuring that only the first letter of each word is uppercase.

JavaScript
// coding beauty console.log('coding BEAUTY'.toLowerCase());

Tip: If it’s not necessary for the remaining letters in each word to be lowercase, you can remove the call to the toLowerCase() method.

Then we call the String split() method on the string to split all the words into an array.

JavaScript
// [ 'welcome', 'to', 'coding', 'beauty' ] console.log('welcome to coding beauty'.split(' '));

After creating the array of words, we call the map() method on it, with a callback function as an argument. This function is called by map() and returns a result for each word in the array.

In the function, we get the word’s first character with charAt(), convert it to uppercase with toUpperCase(), and concatenate it with the rest of the string.

We use the String slice() method to get the remaining part of the string. Passing 1 to slice() makes it return the portion of the string from the second character to the end.

Note: String (and array) indexing is zero-based JavaScript, so the first character in a string is at index 0, the second at 1, and the last at str.length-1

Lastly, we concatenate the words into a single string, with the Array join() method

Passing a space (' ') to join() separates the words by a space in the resulting string.

JavaScript
// Welcome To Coding Beauty console.log(['Welcome', 'To', 'Coding', 'Beauty'].join(' '));

After creating the capitalizeWords() function, we call it as the component is rendered by wrapping it in curly braces ({ }) in our JSX code.

App.js
return ( <div> <b>{str1}</b> <br /> Capitalized: <b>{capitalizeWords(str1)}</b> <br /> <br /> <b>{str2}</b> <br /> Capitalized: <b>{capitalizeWords(str2)}</b> </div> );

The function is invoked and its result is rendered at the point where the curly braces are located.

How to Convert Decimal to Hex in JavaScript

In this article, we’re going to learn how to easily convert a decimal number to its hexadecimal equivalent in JavaScript. And we’ll look at some real-world scenarios where we’ll need to do this.

Number toString() method

To convert a decimal to hex in JavaScript, call the toString() method on the decimal, passing 16 as the radix argument, i.e., num.toString(16). The toString() method will return the string representation of the number in hexadecimal form.

For example:

JavaScript
const num = 60; const hex = num.toString(16); console.log(hex); // 3c // Use parentheses when calling toString() directly const hex2 = (60).toString(16); console.log(hex2); // 3c

The Number toString() method returns the string representation of a number. If a base is specified with the first argument, the number is represented in that base. We pass 16 to use base 16, which is the hexadecimal base.

The hexadecimal base uses 16 symbols to represent numbers:

  • 0 to 9 to represent values 0 to 9
  • a to f (A to F) to represent values 10 to 16. The letters are case-insensitive, so 3C2b is exactly the same value as 3c2B.

Call toString() on number literal

If you call toString() on a number literal directly, ensure you wrap it in parentheses (( )) or use two dots (..before toString():

JavaScript
// Use parentheses const hex2 = (60).toString(16); console.log(hex2); // 3c // Use double dots const hex3 = 50..toString(16); console.log(hex3); // 32

If you use only one dot without parentheses, the JavaScript parser treats it as part of the number literal – a decimal point – instead of a member access operator.

JavaScript
console.log(40.); // 40 console.log(20.); // 20

So there will be an error, since there will be no member access operator before the member name.

JavaScript
// SyntaxError console.log(40.toString(16)); // SyntaxError console.log(20.toString(16));

So you wrap the number in parentheses so that everything outside them are seen as separate from the number.

JavaScript
console.log((40).toString(16)); // 28 console.log((20).toString(16)); // 14

Or you add a second dot that will be seen as the member access operator.

JavaScript
console.log(40..toString(16)); // 28 console.log(20..toString(16)); // 14

Use Case: Convert RGB(A) to Hex

One common use for converting decimal values to hex to convert a RGB color code to its hex equivalent. Here’s how we can do it:

JavaScript
function decToHex(dec) { return dec.toString(16); } function padToTwo(str) { return str.padStart(2, '0'); } function rgbToHex(r, g, b) { const hexR = padToTwo(decToHex(r)); const hexG = padToTwo(decToHex(g)); const hexB = padToTwo(decToHex(b)); return `#${hexR}${hexG}${hexB}`; } console.log(rgbToHex(255, 128, 237)); // #ff80ed console.log(rgbToHex(195, 151, 151)); // #c39797 console.log(rgbToHex(16, 16, 16)); // #0f0f0f

We create a reusable rgbToHex() function to convert the RGB code to its hex equivalent.

We use the padToTwo() function to pad a hex code to two digits, e.g, f -> 0f.

After converting the R, G, and B decimal values to their hexadecimal representations, we join them together in a string prefixed with the # character to form the hex color code.

We could modify the function to also accept RGBA values, where the A is a percentage value (between 0 and 1) used to specify color opacity. A will be the first two characters of the hex color code, having a value between 00 (0 or 0%) and ff (255 or 100%)

JavaScript
function decToHex(dec) { return dec.toString(16); } function padToTwo(str) { return str.padStart(2, '0'); } function rgbToHex(r, g, b, a) { const hexR = padToTwo(decToHex(r)); const hexG = padToTwo(decToHex(g)); const hexB = padToTwo(decToHex(b)); // Set "a" to 1 if not specified const aAbsolute = Math.round((a ?? 1) * 255); const hexA = padToTwo(decToHex(aAbsolute)); return `#${hexA}${hexR}${hexG}${hexB}`; } console.log(rgbToHex(255, 128, 237)); // #ffff80ed console.log(rgbToHex(195, 151, 151, 0.5)); // #80c39797 console.log(rgbToHex(16, 16, 16, 0.69)); // #b0101010

How to Change an Element’s Style on Hover in React

To change an element’s style on hover in React, set a className on the element, and style its :hover pseudo-class.

For example:

App.js
import './App.css'; export default function App() { return ( <div> <div className="box"> <p>Coding Beauty</p> </div> </div> ); }
App.css
.box { height: 100px; width: 100px; background-color: blue; color: white; font-weight: bold; padding: 8px; text-align: center; } .box:hover { background-color: yellow; color: black; }
Hovering over the element changes its style.
Hovering over the element changes its style.

We use the :hover pseudo-class to style an element when the user hovers over it with the mouse pointer.

Change element style on hover with inline styling

We can also change an element’s style on hover using inline styles and the element’s style prop. To do this, we need to create state that will determine whether the hover styles should be applied to the element or not. We also need to add event listeners for the mouseenter and mouseleave and change the state’s value in them.

For example:

App.js
import { useState } from 'react'; export default function App() { const [hover, setHover] = useState(false); const handleMouseEnter = () => { setHover(true); }; const handleMouseLeave = () => { setHover(false); }; return ( <div> <div style={{ height: '100px', width: '100px', backgroundColor: hover ? 'yellow' : 'blue', color: hover ? 'black' : 'white', fontWeight: 'bold', padding: '8px', textAlign: 'center', }} onMouseEnter={handleMouseEnter} onMouseLeave={handleMouseLeave} > <p>Coding Beauty</p> </div> </div> ); }

We use the useState hook to create the boolean state variable that will determine whether the hover styles should be applied to the element or not. useState returns an array of two values. The first is a variable that stores the state, and the second is a function that updates the state when it is called.

We use the onMouseEnter prop to listen for the mouseenter event to detect when the mouse enters within the element’s bounds.

Note: While we could also listen for the mouseover event to detect hover, this event is triggered on an element and every single one of its ancestor elements in the DOM tree (i.e. it bubbles) and this could cause serious performance problems in deep hierarchies. mouseenter doesn’t bubble so we can use it without worrying about this.

Similarly, we use the onMouseLeave prop to listen for the mouseleave to detect when the mouse leaves the element’s bounds.

We use the ternary operator to conditionally set the style based on the boolean state.

Change element style on hover with custom component

If you frequently use the inline styles approach to change the element’s style on hover, it will be better if you encapsulate the logic into a custom component, so you can reuse it in multiple places in your codebase and avoid unnecessary duplication.

Here’s what such a component would like:

Hover.jsx
import { useState } from 'react'; export default function Hover({ children }) { const [hover, setHover] = useState(); const handleMouseEnter = () => { setHover(true); }; const handleMouseLeave = () => { setHover(false); }; return ( <div style={{ display: 'contents' }} onMouseEnter={handleMouseEnter} onMouseLeave={handleMouseLeave} > {children(hover)} </div> ); }

The Hover component takes a callback function as its child. It passes the state variable storing the hover state to this callback so that you can use it to change the style of the element returned from the callback. Hover calls the callback to render this element.

It wraps the element with a div, on which it listens for the mouseenter and mouseleave events to update the state variable. We set the display CSS property to contents on the wrapper because it plays no visual role on the page. It’s only there for the hover detection.

Here’s how we can use our custom Hover component.

Hover.jsx
import Hover from './Hover'; export default function App() { return ( <div> <Hover> {(hover) => ( <div style={{ height: '100px', width: '100px', backgroundColor: hover ? 'yellow' : 'blue', color: hover ? 'black' : 'white', fontWeight: 'bold', padding: '8px', textAlign: 'center', }} > <p>Coding Beauty</p> </div> )} </Hover> </div> ); }

How to Fix the “Cannot access before initialization” Reference Error in JavaScript

The “cannot access before initialization” reference error occurs in JavaScript when you try to access a variable before it is declared with let or const and initialized in the same scope. To fix it, initialize the variables before accessing them.

The "cannot access before initialization" reference error occurring in JavaScript.
The “cannot access before initialization” reference error occurring in JavaScript.

Here are some examples of the error occurring:

index.js
// ❌ ReferenceError: Cannot access 'num' before initialization console.log(num); let num = 2; // ❌ ReferenceError: Cannot access 'name' before initialization console.log(name); const name = 'Coding Beauty'; // ❌ ReferenceError: Cannot access 'arr' before initialization arr = [7, 8, 9]; let arr = [3, 4, 5]; 

To solve the error, you need to initialize the variable before accessing it.

index.js
// ✅ No error let num = 2; console.log(num); // 2 // ✅ No error const name = 'Coding Beauty'; console.log(name); // Coding Beauty // ✅ No error let arr = [3, 4, 5]; arr = [7, 8, 9];

var vs let

The occurrence of this error shows an important difference between the var and let keywords. If you had declared the variable with var, the error wouldn’t have occurred.

index.js
// No error console.log(num); // undefined var num = 2; // No error console.log(name); // undefined var name = 'Coding Beauty'; // No error arr = [7, 8, 9]; var arr = [3, 4, 5];

This happens because variables declared with the var keyword are hoisted – they are moved by the JavaScript interpreter to the top of the current scope (to the top of the current script or current function).

Basically, this is what happens to our variables declared with var:

index.js
var num; var name; var arr; console.log(num); // undefined num = 2; console.log(name); // undefined name = 'Coding Beauty'; arr = [7, 8, 9]; arr = [3, 4, 5];

Note: The declarations are hoisted, but the initializations are not.

Because of this hoisting, even when the variable is declared in a non-function block, like an if block, the error will still not occur if you try to access the variable from outside the if statement.

index.js
// No error! console.log(num); // undefined if (true) { var num = 2; }

Only the global scope and function scopes are used for hoisting, so like before, the variable declaration is moved to the top of the file.

index.js
var num; console.log(num); // undefined if (true) { num = 2; }

How to Get the Window’s Width and Height in React

To get the width and height of the window in React, use window.innerWidth and window.innerHeight.

App.js
import { useRef } from 'react'; export default function App() { const windowSize = useRef([window.innerWidth, window.innerHeight]); return ( <div> <h2>Width: {windowSize.current[0]}</h2> <h2>Height: {windowSize.current[1]}</h2> </div> ); } 
Getting the window width and height in React.

The innerWidth property returns the interior width of the window in pixels, including the width of the vertical scrollbar, if it’s present.

Similarly, innerHeight property returns the interior height of the window in pixels, including the height of the horizontal scrollbar, if it is present.

Since we are getting the window width and height only once – when the component mounts – we use the useRef() hook to store them.

useRef returns a mutable ref object that doesn’t change its value when a component is updated. Also, modifying the value of this object’s current property does not cause a re-render. This is in contrast to the setState update function returned from useState.

Get window width and height on resize in React

To get the window width and height on resize in React:

  1. Get the window width and height with window.innerWidth and window.innerHeight.
  2. Add an event listener for the resize event on the window object.
  3. Update the window width and height in this event listener.

In the previous example, we needed to get the window width and height only once, and we used a ref to store it.

If you instead want to get the window’s width and height when it is resized, you’ll need to add a resize event listener to the window object and create a state variable to track changes to the width and height.

App.js
import { useState, useEffect } from 'react'; export default function App() { const [windowSize, setWindowSize] = useState([ window.innerWidth, window.innerHeight, ]); useEffect(() => { const handleWindowResize = () => { setWindowSize([window.innerWidth, window.innerHeight]); }; window.addEventListener('resize', handleWindowResize); return () => { window.removeEventListener('resize', handleWindowResize); }; }, []); return ( <div> <h2>Width: {windowSize[0]}</h2> <h2>Height: {windowSize[1]}</h2> </div> ); }
The window’s width and height get updated when it’s resized.

We use the useState React hook to create a state variable that will be updated whenever the height or width of the window changes.

The useState hook returns an array of two values. This first is a variable that stores the state, and the second is a function that updates the state when it is called.

JavaScript
const [windowSize, setWindowSize] = useState([ window.innerWidth, window.innerHeight, ]);

The useEffect hook is used to perform an action when a component first renders, and when one or more specified dependencies change. In our example, the action is to add the event listener for the resize hook with the addEventListener() method.

We pass an empty dependencies array to the useEffect, so that it gets called only once in the component’s lifetime, and the resize event listener is only registered once – when the component first renders.

App.js
useEffect(() => { const handleWindowResize = () => { setWindowSize([window.innerWidth, window.innerHeight]); }; window.addEventListener('resize', handleWindowResize); return () => { window.removeEventListener('resize', handleWindowResize); }; }, []);

In the resize event listener, we update the state variable with the new height and width of the window.

The function we return in useEffect is a function that performs clean-up operations in the component. We use the removeEventListener() method to remove the resize event listener in this clean-up function and prevent a memory leak.

Note

useEffect‘s cleanup function runs after every re-render where its dependencies change in addition to when the component unmounts. This prevents memory leaks that happen when an observable prop changes value without the observers in the component unsubscribing from the previous observable value.

Since we pass an empty dependencies array, its dependencies won’t ever change, so in this case, it’ll run only when the component unmounts.

How to Convert Seconds to Hours and Minutes in JavaScript

To convert seconds to hours and minutes in JavaScript:

  1. Divide the seconds by 60 to get the total minutes. The remainder will be the seconds in the output.
  2. Divide the total minutes by 60 to get the hours. The whole number in the result will be the hours, and the remainder will be the minutes in the output.

For example:

function toHoursAndMinutes(totalSeconds) {
  const totalMinutes = Math.floor(totalSeconds / 60);

  const seconds = totalSeconds % 60;
  const hours = Math.floor(totalMinutes / 60);
  const minutes = totalMinutes % 60;

  return { h: hours, m: minutes, s: seconds };
}

// { h: 0, m: 1, s: 0 }
console.log(toHoursAndMinutes(60));

// { h: 0, m: 16, s: 40 }
console.log(toHoursAndMinutes(1000));

// { h: 1, m: 10, s: 50 }
console.log(toHoursAndMinutes(4250));

We create a reusable function that takes a total number of seconds and returns an object containing the separate hours, minutes, and seconds values.

First, we divide the total seconds by 60 to get the number of total minutes.

console.log(60 / 60); // 1

console.log(1000 / 60); // 16.666666666666668

console.log(4250 / 60); // 70.83333333333333

The division results in a decimal number, so we use the Math.floor() function to get just the whole number of the result.

Math.floor() returns the largest integer less than or equal to a specified number.

console.log(Math.floor(10.95)); // 10
console.log(Math.floor(10)); // 10
console.log(Math.floor(10.05)); // 10

After this, we use the modulo operator (%) to get the division’s remainder for the seconds in the result.

console.log(60 % 60); // 0

console.log(1000 % 60); // 40

console.log(4250 % 60); // 50

Tip: Skip this step if the seconds should not be in the result.

We all know 60 seconds make a minute, and 60 minutes make an hour. After getting the total minutes, we do something similar to what we just did to the total seconds value: we divide the total minutes by 60 to get the hours and use the module operator to get the division’s remainder, which will be the minutes in the output.

After getting all the values, we return an object with h, m, and s properties containing the hours, minutes, and seconds respectively.

return { h: hours, m: minutes, s: seconds };

Convert seconds to HH:mm:ss

Instead of an object, you might want the result to be a time string in a format like HH:mm:ss, where each value is separated by a colon and has a minimum of two digits in the string. Here’s how we can produce such a time string.

function toTimeString(totalSeconds) {
  const totalMs = totalSeconds * 1000;
  const result = new Date(totalMs).toISOString().slice(11, 19);

  return result;
}

console.log(toTimeString(60)); // 00:01:00

console.log(toTimeString(1000)); // 00:16:40

console.log(toTimeString(4250)); // 01:10:50

First, we convert the seconds to milliseconds to pass it to the Date() constructor and create a new Date object.

The Date toISOString() method returns a string representation of the date in the ISO 8601 format, i.e., YYYY-MM-DDTHH:mm:ss.sssZ

const totalMs = 60000;

// 1970-01-01T00:01:00.000Z
console.log(new Date(totalMs).toISOString());

You can see that toISOString() does the conversion for us. We just need a way to extract the HH:mm:ss from the ISO string.

The String slice() method returns the portion of a string between specified start and end indexes, passed as the first and second arguments respectively.

The HH:mm:ss starts at index 11 of the string and ends at index 18. So to extract it, the values we pass to slice() or substring() will be 11 and 19.

Tip: 19 because slice() excludes the character at the end index from the resulting substring.

// 00:01:00
console.log('1970-01-01T00:01:00.000Z'.slice(11, 19));

To exclude the seconds from the result, we just need to reduce the end index by 3 to exclude the colon and the digits of the seconds.

// 00:01
console.log('1970-01-01T00:01:00.000Z'.slice(11, 16));

How to Easily Handle the onScroll Event in React

To handle the onScroll event on a React element, assign a function to the onScroll prop of the element and use the event object to perform an action. That action will occur whenever the user scrolls up or down on the page.

For example:

App.jsx

import { useState } from 'react';

export default function App() {
  const [scrollTop, setScrollTop] = useState(0);

  const handleScroll = (event) => {
    setScrollTop(event.currentTarget.scrollTop);
  };

  return (
    <div>
      Scroll top: <b>{scrollTop}</b>
      <br />
      <br />
      <div
        style={{
          border: '1px solid black',
          width: '400px',
          height: '200px',
          overflow: 'auto',
        }}
        onScroll={handleScroll}
      >
        {[...Array(10)].map((_, i) => (
          <p key={i}>Content</p>
        ))}
      </div>
    </div>
  );
}
The text is updated when the component's onScroll event fires.
The text is updated when the component’s onScroll event fires.

The function (event listener) passed to the onScroll prop is invoked whenever the viewport is scrolled. It is called with an event object, which you can use to perform actions and access information related to the scroll event.

The currentTarget property of this event object returns the element that the onScroll listener was attached to.

Tip: If you’re not sure of when to use the event object’s currentTarget or target properties, this article might help: Event target vs currentTarget in JavaScript: The Important Difference.

We use the element’s scrollTop property to get how far the element’s scrollbar is from its topmost position. Then we update the state variable with the new value, and this reflects on the page.

Note: We used the useState hook to manage the state. This hook returns an array of two values, where the first is a variable that stores the state, and the second is a function that updates the state when it is called.

Handle onScroll event on window object in React

We can also handle the onScroll event on the global window object, to perform an action when the viewport is scrolled. We can do this with the addEventListener() method:

App.js

import { useState, useEffect } from 'react';

export default function App() {
  const [scrollTop, setScrollTop] = useState(0);

  useEffect(() => {
    const handleScroll = (event) => {
      setScrollTop(window.scrollY);
    };

    window.addEventListener('scroll', handleScroll);

    return () => {
      window.removeEventListener('scroll', handleScroll);
    };
  }, []);

  return (
    <div>
      <div
        style={{
          position: 'fixed',
          padding: '10px 0',
          top: '0',
          backgroundColor: 'white',
          borderBottom: '1px solid #c0c0c0',
          width: '100%',
        }}
      >
        Scroll top: <b>{scrollTop}</b>
      </div>
      <div style={{ marginTop: '50px' }}>
        {[...Array(30)].map((_, i) => (
          <p key={i}>Content</p>
        ))}
      </div>
    </div>
  );
}
The text is updated when the window's onScroll event fires.
The text is updated when the window’s onScroll event fires.

The addEventListener() method takes up to two arguments:

  1. type: a string representing the event type to listen for.
  2. listener: the function called when the event fires.

It also takes some optional arguments, which you can learn more about here.

We call addEventListener() in the useEffect hook to register the listener once the component renders as the page loads. We pass an empty dependencies array to useEffect so this registration happens only once. In the cleanup function, we call the removeEventListener() method to unregister the event listener and prevent a memory leak.

In the onScroll listener, we access the Window scrollY property to display the number of pixels that the page has currently scrolled horizontally.

How to Capitalize the First Letter of a String in Vue.js

To capitalize the first letter of a string in Vue:

  1. Get the string’s first letter with [0].
  2. Uppercase this letter with .toUpperCase().
  3. Get the rest of the string with .slice(1).
  4. Add the results together.

For example:

App.vue

<template>
  <div id="app">
    <b>{{ name1 }}</b>
    <br />
    Capitalized: <b>{{ capitalized(name1) }}</b>
    <br /><br />
    <b>{{ name2 }}</b
>
    <br />
    Capitalized:
    <b>{{ capitalized(name2) }}</b>
  </div>
</template>

<script>
export default {
  data() {
    return { name1: 'coding beauty', name2: 'javascript' };
  },
  methods: {
    capitalized(name) {
      const capitalizedFirst = name[0].toUpperCase();
      const rest = name.slice(1);

      return capitalizedFirst + rest;
    },
  },
};
</script>
The strings and their capitalized forms.
The strings and their capitalized forms.

We create a reusable Vue instance method (capitalized) that takes a string and capitalizes its first letter.

We use bracket indexing ([ ]) to get the 0 property of the string – the character at index 0. String (and array) indexing is zero-based in JavaScript, so the string’s first character is at index 0, the second is at index 1, and so on, until the last character at index str.length-1.

After getting the string’s first character, we uppercase it with the toUpperCase() method.

We use the slice() method to get the rest of the string. slice() returns the portion of a string between a specified start and an optional end index. If the end index is omitted, the substring will range from the start index to the end of the string.

Hence, slice(1) returns a substring ranging from the second character to the end.

After getting the remaining part of the string, all that’s left is simply concatenating it with the capitalized first letter.

If it’s necessary for the resulting string to have only its first letter capitalized, you can call the toLowerCase() method on the result of slice(), to lowercase the rest of the string before concatenation.

const capitalizedFirst = name[0].toUpperCase();

// 👇 toLowerCase()
const rest = name.slice(1).toLowerCase();

return capitalizedFirst + rest;

Use Computed Property

If you’re trying to capitalize a string data property of the Vue instance (like in our example), you can use a computed property in place of a method.

<template>
  <div id="app">
    <b>{{ name1 }}</b>
    <br />
    Capitalized: <b>{{ capitalizedName1 }}</b> <br /><br />
    <b>{{ name2 }}</b
    ><br />
    Capitalized:
    <b>{{ capitalizedName2 }}</b>
  </div>
</template>

<script>
export default {
  data() {
    return { name1: 'coding beauty', name2: 'javascript' };
  },
  methods: {
    capitalized(name) {
      const capitalizedFirst = name[0].toUpperCase();
      const rest = name.slice(1);

      return capitalizedFirst + rest;
    },
  },

  // 👇 Computed properties
  computed: {
    capitalizedName1() {
      return this.capitalized(this.name1);
    },
    capitalizedName2() {
      return this.capitalized(this.name2);
    },
  },
};
</script>

Using a computed property for this is useful when the data is likely to change in the app’s lifetime, e.g., in a two-way binding with an input field. When the data property changes, the depending computed property will be recalculated and updated automatically.

How to Capitalize the First Letter of Each Word in an Array in JavaScript

To capitalize the first letter of each word in an array in JavaScript:

  1. Iterate over the words array with .map().
  2. For each word, return a new word, the uppercased form of the word’s first letter added to the rest of the word.

For example:

function capitalizeWords(arr) {
  return arr.map((word) => {
    const capitalizedFirst = word.charAt(0).toUpperCase();
    const rest = word.slice(1).toLowerCase();
    return capitalizedFirst + rest;
  });
}

const words = ['welcome', 'to', 'CODING', 'Beauty'];

// [ 'Welcome', 'To', 'Coding', 'Beauty' ]
console.log(capitalizeWords(words));

Our capitalizeWords() function takes an array of words and returns a new array with all the words capitalized, without mutating the original array.

Firstly, we call the map() method on the array, passing a callback function as an argument. This function will be called and return a result for each word in the array.

In the callback, we get the word’s first character with charAt(), convert it to uppercase with toUpperCase(), and concatenate it with the rest of the string.

We use the String slice() method to get the remaining part of the string. Passing 1 to slice() makes it return the portion of the string from the second character to the end.

Note: String (and array) indexing is zero-based JavaScript, so the first character in a string is at index 0, the second at 1, and the last at str.length-1

After capitalizing the word, we lowercase it with the String toLowerCase() method. You can remove this toLowerCase() call if it’s not necessary for the non-first letter of each word in the array to be capitalized.

How to Capitalize the First Letter of Each Word in a String in JavaScript

To capitalize the first letter of each word in a string in JavaScript:

  1. Split the string into an array of words with .split('').
  2. Iterate over the words array with .map().
  3. For each word, return a new word that is an uppercase form of the word’s first letter added to the rest of the word.
  4. Join the words array into a string with .join(' ').

For example:

index.js

function capitalizeWords(str) {
  return str
    .toLowerCase()
    .split(' ')
    .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
    .join(' ');
}


// Welcome To Coding Beauty
console.log(capitalizeWords('WELCOME to coding beauty'));

// JavaScript And TypeScript
console.log(capitalizeWords('JAVASCRIPT AND TYPESCRIPT'));

Our capitalizeWords() function takes a string and returns a new string with all the words capitalized.

First, we use the toLowerCase() method to lowercase the entire string, ensuring that only the first letter of each word is uppercase.

// welcome to coding beauty
console.log('WELCOME to coding beauty'.toLowerCase());

Tip: If it’s not necessary for the remaining letters in each word to be lowercase, you can remove the call to the toLowerCase() method.

Then we call the String split() method on the string to split all the words into an array.

// [ 'welcome', 'to', 'coding', 'beauty' ]
console.log('welcome to coding beauty'.split(' '));

After creating the array, we call the map() method on it, with a callback function as an argument. This function will be called and return a result for each word in the array.

In the function, we get the word’s first character with charAt(), convert it to uppercase with toUpperCase(), and concatenate it with the rest of the string.

We use the String slice() method to get the remaining part of the string. Passing 1 to slice() makes it return the portion of the string from the second (index of 1) character to the end.

// [ 'Welcome', 'To', 'Coding', 'Beauty' ]
console.log(
  'welcome to coding beauty'
    .split(' ')
    .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
);

So map() returns an array containing all the words in the string, with each word’s first letter capitalized.

Lastly, we concatenate all the words in a single string, with the Array join() method.

Passing a space (' ') to join() separates the words by a space in the resulting string.

// Welcome To Coding Beauty
console.log(['Welcome', 'To', 'Coding', 'Beauty'].join(' '));