This post will discuss how to append a char to the end of a string in JavaScript.

There are several ways to append a char to the end of a string in JavaScript. A char is a single unit of text, such as a letter, a digit, or a symbol. A string is a sequence of zero or more chars, enclosed by single or double quotes. To append a char to the end of a string, we need to concatenate the string and the char using some operator or function. Here are some of the most common and easy-to-use functions for appending a char to the end of a string in JavaScript:

1. Using Addition operator

This is a binary operator that performs addition when used with numbers, or concatenation when used with strings. We can use this operator to append a char to the end of a string by adding the char as a string after the original string. For example, if we want to append the char "!" to the end of the string "Hello", we can write:

Download  Run Code

 
We can also use shorthand assignment operators (+=) to append the char to the original string without creating a new variable. It assigns the result of adding the character to the string back to the original string variable, like this:

Download  Run Code

2. Using concat() function

The concat() is an instance function of the String object that returns a new string that is the concatenation of one or more strings. We can use this function to append a char to the end of a string by calling it on the original string and passing the char as a string as an argument. For example, if we want to append the char "!" to the end of the string "Hello", we can write:

Download  Run Code

 
We can also pass multiple chars or strings as arguments to create a longer string. Here’s an example:

Download  Run Code

3. Using Template literals

These are special strings that allow embedded expressions and multi-line strings. We can use template literals to append a char to the end of a string by enclosing both the string and the char in backticks ( ), and using the dollar sign and curly braces ($ {}) to indicate an expression. For example, if we want to append the char “!” to the end of the string “Hello”, we can write:

Download  Run Code

 
We can also use template literals to create complex strings with variables, expressions, and formatting. Here’s an example:

Download  Run Code

That’s all about appending a char to the end of a string in JavaScript.