DEV Community

Cover image for Understanding Strings in JavaScript
Mwendwa Bundi Emma
Mwendwa Bundi Emma

Posted on

Understanding Strings in JavaScript

What are strings? Strings are basic data types in JavaScript. They do sequence one or more characters whether letters, numbers or symbols.

Strings are always enclosed in single quotes and doubles quotes which we look at today. However, you can use backticks, a case termed as template literal strings which I'll explain in another article.

Take a look at these;

'single quoted string'
"double quoted string"

The quotes used at the start of the string should be used at the end too

Simple, right?, now, let's declare a string variable

You know that variables in JS use the var, const & let keywords... Now, lets use the** myNeeds** variable to store our string

example:

var myNeeds = "I need books and pens";

Declaring the same string variable in single quotes will give the same output

example:

var myNeeds = 'I need books and pens';

console.log(myNeeds);

output: I need books and pens

String literals and string values difference

A string literal is what you type into your source code. The string value is what gets output when you print it.

"I need books and pens" is a string literal.
The console.log output which will be I need books and pens is a string value.

Escaping literal quotes in strings

There are times you will need to escape quotes in your string so as to avoid errors. Say a case like:

'we're here but they're over there.'
or say you need to quote what someone said in your string...

To avoid JS errors, you sure need to use the backslash before the quote.

How?

'we're here but they\'re over there.'

The above case output would be:

we're here but they're over there.
or

"she said, \"we are doing good today\", as we were talking.";

The above case output would be:
she said, "we are doing good today", as we were talking.

What the backslash** \ **means is that the particular quote is not the end of the string but it should appear in the string value instead.

Sometimes you could choose to use the alternate string syntax which also works just fine.

Example;
"we're here but they're over there."
'she said, "we are doing good today", as we were talking.'

String concatenation

When joining two or more strings together, we use the concatenation operator which uses the + symbol.

var myNeeds = "I need books " + "and pens"
output:

I need books and pens

But take note of this whitespace case like:

"java " + "script" would output:
java script
While "java" + "script" would output:

javascript

Notice the difference and how to use the whitespace character.

Strings can also be concatenated using the += operator

example:
let myNeeds = "I need books. ";

myNeeds += "I also need pens too.";

Our output here for myNeeds would be:

I need books. I need pens too.

Finally, remember we said we can escape quotes by use of a backslash...We can also escape other characters like:

\r to get carriage return

\n to get to a newline

\t to get the tab spacing....to show case but a few.

For your output to show a single backslash you would then need to use two backslashes.

Conclusion

Today we were able to learn about strings in JavaScript. Remember, at the beginning I mentioned template literal strings which I will also write on as a separate article so as to do an in depth covering on the same.

Todays quote, "whether you want to uncover the secrets of the universe, or you want to pursue a career in the 21st century, basic computer programming is an essential skill to learn." Stephen Hawking

Thank you for reading, comment below and let me know what you think. Bye for now.

Top comments (0)