Learn about Javascript Strings

We will learn how to create Strings, escaping quotes and string concatenation.

Code examples below are also available at Github

Creating a String
Strings are surrounded by quotation marks. It can be single or double quotes.
Just like we declare and initialize numbers we can work with strings.

let message = 'Hello World';

Here message is a string literal.

Escaping Characters in a String
Let’s say we need to use the following string – 'I'm at the store right now'. As you can see here we have a single quote in the string itself. This won’t work.
To handle this we can use the \ symbol.
let message = 'I\'m at the store right now'
Using \’ solves the problem.

Joining two strings
To join or concatenate two strings we can use + operator.
let str1 = 'Hello';
let str2 = 'Sam';

str1+' '+str2

To convert a string to a number you can use the Number() function.
To convert a number to a string you can use toString()

let numStr = '922';

let num = Number(numStr);

let tempStr = num.toString();

Template Literals or Template Strings
This is a new addition. Using this it’s convenient to concatenate strings or print out a variable value.
We use backtick instead of quotes.
let fName = 'Raj';
let message = Check this out. My name is ${fName};

Author: Ankur

I am Enthusiastic about Learning new things and sharing my Knowledge. I like programming and have a pretty good background in Computer Science.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: