How to Use String Literals in JavaScript?

Estimated read time 1 min read

In JavaScript, string literals are sequences of characters surrounded by single quotes (') or double quotes ("). Here are a few examples of string literals in JavaScript:

'Hello, World!'
"Hello, World!"
"This is a string with a \"quote\" inside."
'This is a string with a \nnew line.'

You can use string literals to represent text, for example, as the value of a variable:

const message = "Hello, World!";
console.log(message);

Or as an argument in a function call:

function printMessage(message) {
  console.log(message);
}

printMessage("Hello, World!");

In addition to the standard use of string literals, JavaScript also provides some string manipulation methods, such as concat, slice, substring, and replace, which can be used to manipulate strings in various ways.

Here’s an example of using the concat method:

const greeting = "Hello";
const name = "John";
const message = greeting.concat(", ", name, "!");
console.log(message);

This will output the following:

Hello, John!

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply