Javascript Strings – Learn How To Create Strings


First of all, I would like to give you some real-life examples where we use strings, before going to learn about javascript strings.

When searching on the google or any other website, the text you entered on the search box is a string.

While you chatting, the text you entered on the input box is a string.

Without using strings, you can’t see any text on your code.

A sequence of characters written in single or double quotes is known as the javascript string.

Creating Javascript Strings.

Method 1:

Creating a string is very easy in javascript. Just write the text within a single or double quote’s.

Syntax:

var string_name = " string_value ";

For example: 

 var  msg = " hello world ";

In the above example, ‘hello world’ is the string which is placed in double quotes.

var msg = 'welcome';

Here the ‘welcome’ is also a string which is placed in single quotes.

Method 2:

You can also create a string using String constructor by passing string value.

Syntax:

var string_name = new String(" string_value "); 

Example:

var str = new String("welcome");

In the above example, We create an instance of string class by passing string value “welcome” and stored it in variable str.

as a result, the value of str will be “welcome”.

If you don’t familiar with object-oriented programming concepts it may be a bit handy to you. But don’t worry, just remember the syntax otherwise you can use method 1.

The first method is the basic and best approach for creating a javascript string.

Examples:     var msg = " Hello world ";

var msg = ' Hello world '; 

In the above examples, both are valid strings. There is a strict rule applying to quotes while forming javascript strings. That is you might use both double quotes or single quotes but don’t use the single quote with a double quote or double quote with a single quote.

For Example:

 var msg = ' welcome ";
 var msg = " welcome ';

In the above examples, both are invalid javascript strings. Javascript interpreter produces an error in this case.

Uncaught SyntaxError: Invalid or unexpected token

Escape characters in Javascript Strings:

Anything written in single or double quotes will be considered as a javascript string.

Even If you write numbers on quotes, it will also consider as a string by javascript interpreter.  But there are some special characters to be encoded in strings means if you write those characters in strings it will be outputted in a different manner.

For Example:

var new_line = " go to \n new line ";

Let’s consider the above example if you try to display the value of new_line go to will display on the first line, and new line will display on the second line. For better understanding see the screenshot below.

javascript strings - new line example screenshot

Similar to the \n, there are few more escape characters in javascript. I list them below have a look at on it.

List of escape characters in javascript

Character Result
\'  single quote
\"  double quote
\\  backslash
\n  new line
\r  carriage return
\v  vertical tab
\b  backspace
\f  form feed

 

Below are the few examples of escape notation.

var my_name = 'south joovins blog';

For example, In the above statement I want to put a single quote in between joovin and s.

But, If you write like this 'south joovin's blog' will produce an error.

So, you need to write 'south joovin\'s blog'. In this case, \' will be encoded as a single quote( ' ).

Also, you can add double quotes in your strings like below.

'south joovin\"s blog'   will result as   south joovin"s blog

And you can also add backslashes in your string

'south joovin\'s blog \\'.  results south joovin's blog \

Similarly, all the escape characters work in their own manner.

Multi-line Javascript Strings.

Sometimes, maybe your javascript strings may be a bit longer. When you can’t write them in a single line. In those cases, you need to split javascript strings into multiple lines.

If you split javascript strings into multiple lines you need to concat them with the ( + ) operator.

For example:   var message = " Hey my name " +

" is south joovin " +

" I am a Javascript Programmer " ;

Here the ( + ) operator clubbing each string into a single one.

Therefore the output of the message will be

Hey my name is south joovin I am a Javascript Programmer

Finally, I hope now you understand the concept of javascript strings. It’s an easy topic compared to other  javascript topics.

If you like this article please share with your friends and if you have any doubts feel free to comment below.

Try other Javascript tutorials on my blog.

  1. Learn Javascript in one day
  2. Javascript Operators
  3. Javascript Variables

If you want to learn more about javascript, below I listed the free ebooks. Have a look.

  1. Eloquent Javascript
  2. The Javascript Way
  3. Javascript For Cats
  4. Speaking Javascript
  5. Thinking In Javascript

Leave a Reply

Your email address will not be published. Required fields are marked *