Strings in JavaScript
Oct 20, 2020 08:23 0 Comments Javascript PARTH

                                           Strings in JavaScript

JavaScript strings are primitive values. JavaScript strings are also immutable. It means that if you process a string, you will always get a new string. The original string doesn’t change.

To create literal strings in JavaScript, you use single quotes or double quotes:

let str = 'Hi';

let greeting = "Hello";

ES6 introduced template literals that allow you to define a string backtick (`) characters:

let name = 'John';

let message = ‘Hello ${name}’;

console.log(greeting);

Output:

Hello John

The string message evaluates the name variable and returns the result string.

Escaping special characters

To escape special characters, you use the backslash \ character. For example:

  • Windows line break: '\r\n'
  • Unix line break: '\n'
  • Tab: '\t'
  • Backslash '\'

The following example uses the backslash character to escape the single quote character in a string:

let str = 'I\'m a string!';

 

Getting the length of the string

The length property returns the length of a string:

let str = "Good Morning!";

console.log(str.length);  // 13

 

Accessing characters

To access the characters in a string, you use the array-like [] notation with the zero-based index.

The following example returns the first character of a string with the index zero:

let str = "Hello";

console.log(str[0]); // "H"

To access the last character of the string, you use the length - 1 index:

let str = "Hello";

console.log(str[str.length -1]); // "o"

 

Concatenating strings via + operator

To concatenate two or more strings, you use the + operator:

let name = 'John';

let str = 'Hello ' + name;

console.log(str); // "Hello John"

If you want to assemble a string piece by piece, you can use the += operator:

let className = 'btn';

className += ' btn-primary'

className += ' none';

console.log(className);

Output:

btn btn-primary none

 

Converting values to string

To convert a non-string value to a string, you use one of the following:

  • String(n);
  • ” + n
  • n.toString()

Note that the toString() method doesn’t work for undefined and null.

When you convert a string to a boolean, you cannot convert it back via the Boolean():

let status = false;

let str = status.toString(); // "false"

let back = Boolean(str); // true

In this example:

  • First, the status is a boolean variable.
  • Then, the toString() returns the string version of the status variable, which is false.
  • Finally, the Boolean() converts the "false" string back to the Boolean that results in true because "false" is a non-empty string.

Note that only string for which the Boolean() returns false, is the empty string (”);

 

Comparing strings

To compare two strings, you use the operator >, >=, <, <=, and == operators.

These operators compare strings based on the numeric values of JavaScript characters. In other words, it may return the string order that is different from the one used in dictionaries.

let result = 'a' < 'b';

console.log(result); // true

However:

let result = 'a' < 'B';

console.log(result); // false

 

Prev Next
About the Author
Topic Replies (0)
Leave a Reply
Guest User

You might also like

Not sure what course is right for you?

Choose the right course for you.
Get the help of our experts and find a course that best suits your needs.


Let`s Connect