In JavaScript any set of symbols forms a string. Unlike other programming languages, JS doesn’t have such a separate data type as a character. Nowadays there are several common ways of character encoding, yet UTF-16 is prevalent in this language.

In order to create a string you can put your text inside either single or double or back quotes. In most cases these ways of specifying a string are interchangeable. However, if you need to create a string of several lines, only back quotes can help you with that as shown in the example below.

Syntax

string1 = "string 1";
string1 = "string 1"
string2 = 'string 2'
string3 = `string 3
                    string 3
                       string 4`;

The use of backticks has one significant advantage which is a possibility to insert a variable into a string. Very often you might need to include into your text some information which is unknown yet or which is changeable. For example, it can be a value returned by some function. In this case you just need to put the name of your function inside the curly brackets and put a dollar sign in front: ${function}.

Let’s create a method that returns a difference of two numbers, then we will try to call it inside the string.

function difference (x, y) {
return x-y;
}
console.log(`6 - 2 = ${difference(6, 2)}`);
// the output is "6 - 2 = 4"

Note that this code will not work if you wrap your string into single or double quotes.

console.log("6 - 2 = ${difference(6, 2)}");
// the output is "6 - 2 = ${difference(6, 2)}"

It’s as well possible to put inside the curly brackets any variable or operator and it will work properly. JavaScript considers such text as a code.

Special symbols

If for some reason you are eager to use single or double quotes for a text with multiple lines, one of the special symbols can help you with that. Simply put \n wherever you need to begin a new line. In the code below we can see how strings specified in different ways can be equal.

string1 = "New\nline";
string2 = `New
line`;
console.log(string1==string2);
// The output is "true"

There are many other special symbols which may help to format your string. For example, in case you need to include a quotation mark into your text, just put a backslash in front of it: \” or \’. Use \t for tabulation.

Another invaluable feature is a possibility of inserting a Unicode script for some characters that you cannot find on your keyboard. For example, let’s create a string with a cent symbol. You only need to find its code from the list of Unicode characters and put a backslash in front of it: \u00A2.

string1 = "50\u00A2";
console.log(string1);
// The output is "50¢"

String formatting using concatenation

Concatenation is a very convenient feature that allows you to link several strings together. The basic operator for appending a string is a plus. Note that the plus operator does not insert any spaces between the two strings so be careful while using it for creating a single sentence of several words. You can simply resolve this issue by adding another string containing only a space.

string1 = "Hello";
string2 = "World"
console.log(string1 + string2); // The output is "HelloWorld"
console.log(string1 + " " + string2); // The output is "Hello World"

One more way to join your strings is using an assignment operator. This is a good choice if you want to change the existing string by adding some additional text to it. Although it might be tricky since you will not have any access to the initial string anymore. In the below example you can see that the string “Hello” is lost after concatenation.

string1 = "Hello";
string2 = "World";
string1 += string2;
console.log(string1); // The output is "HelloWorld"

Concatenation is possible not only with strings but with other data types as well. You can append a number to a string in the same way as above.

age = 25;
console.log("I am " + age); // The output is "I am 25"

Nevertheless, you need to be very attentive while concatenating several variables using a plus operator not to mix it up with getting a sum. The example below illustrates a confusion very common among beginners. You may expect that the result will be 8. However, JavaScript performs all operations from left to right and you will get the result of concatenating instead of calculating.

number1 = 3;
number2 = 5;
console.log("The sum of 3 and 5 is " + number1 + number2); 
// logs "The sum of 3 and 5 is 35"

To resolve the problem, you can either write your expression for calculating the sum first or put your numeric variables inside the brackets.

console.log(number1 + number2 + " is the result"); // 8 is the result
console.log("The result is " + (number1 + number2)); // The result is 8

Converting date and time to a string format

JavaScript offers special methods for proper representation of date and time. If you try to get the current date by creating an object of Date() and log it in the console, you will see the day, date, time and the time zone altogether.

In order to format the string and get only time in the output you can use the toTimeString() method. Call the toDateString() method if you need to show the date only.

date = new Date();
console.log(date.toTimeString()); // 13:55:03 GMT+0200 (Eastern European Standard Time)
console.log(date.toDateString()); // Wed Feb 02 2022

There are even more methods for formatting date strings available in JavaScript. If you need to get the Universal Coordinated Time (UTC), use the toUTCString() method while the toISOString() method will help you to get the ISO date format.

date = new Date();
console.log(date.toUTCString()); // Wed, 02 Feb 2022 12:19:06 GMT
console.log(date.toISOString()); // 2022-02-02T12:19:06.724Z

There is a very comprehensive and flexible toolkit for working with strings in JavaScript. Even though it does not provide a special function for formatting strings as other programming languages do, with the help of available features and methods you can easily format your text to get the desired result.