Introduction to JavaScript Functions

The main logic of the character counter application can be written in the form of functions using JavaScript. This defines logic for dynamically calculating the number of characters and words in the application whenever the user inputs the text. Let’s look into five main functions we used for this application.

useState()

  • It allows you to add state to functional components.
  • It's used to store and update dynamic values in the component.
  • Syntax:
const [state, setState] = useState(initialValue);

replace()

  • It allows you to replace part of a string with another string.
  • It's used to substitute specified characters or substrings in a string.
  • Syntax:
string.replace(searchValue, newValue)

trim()

  • It allows you to remove whitespace from both ends of a string.
  • It's used to clean up strings by removing extra spaces at the start and end.
  • Syntax:
string.trim()

split()

  • It allows you to divide a string into an array of substrings.
  • It's used to break a string into parts using a specified separator.
  • Syntax:
string.split(separator)