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.
var string_name = " string_value ";
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.
You can also create a string using String constructor by passing string value.
var string_name = new String(" string_value ");
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.
var msg = " Hello world ";
var msg = ' Hello world ';
var msg = ' welcome "; var msg = " welcome ';
Uncaught SyntaxError: Invalid or unexpected token
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.
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
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.
( + ) operator.
var message = " Hey my name " +
" is south joovin " +
Here the ( + ) operator clubbing each string into a single one.
Therefore the output of the message will be
If you like this article please share with your friends and if you have any doubts feel free to comment below.