Append a char to end of a string in JavaScript
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:
|
1 2 3 4 5 |
let str = "Hello"; let char = "!"; let result = str + char; console.log(result); // "Hello!" |
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:
|
1 2 3 4 5 |
let str = "Hello"; let char = "!"; str += char; console.log(str); // "Hello!" |
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:
|
1 2 3 4 5 |
let str = "Hello"; let char = "!"; let result = str.concat(char); console.log(result); // "Hello!" |
We can also pass multiple chars or strings as arguments to create a longer string. Here’s an example:
|
1 2 3 4 |
let str = "Hello"; let result = str.concat(" ", "world", "!"); console.log(result); // "Hello world!" |
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:
|
1 2 3 4 5 |
let str = "Hello"; let char = "!"; let result = `${str}{char}`; console.log(result); // "Hello!" |
We can also use template literals to create complex strings with variables, expressions, and formatting. Here’s an example:
|
1 2 3 4 5 6 7 |
let name = "Rudy"; let age = 25; let result = `${name}, you are ${age} years old today!`; // "Rudy, you are 25 years old today!" console.log(result); |
That’s all about appending a char to the end of a string in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)