Guide to JavaScript Strings By Examples

JavaScript strings; Through this tutorial, i am going to explain you about the JavaScript strings with examples.

Guide to JavaScript Strings

  • About JavaScript String
  • Remove special characters in a string javascript
  • How to get the length of the string In javaScript
  • How to Get Character in String JavaScript
  • How to Concatenation String in Javascript Using + Operator
  • How to convert Any Data type to String in JavaScript
  • JavaScript String Split to Array
  • How to Compare Strings in JavaScript

About JavaScript String

A string is a sequence of characters in javaScript programming and which is written within single or double quotations. E.g. “Hello JavaScript Programmer” and ‘Mg578dsfF’ are examples of strings

As mention above, To create a string in js using single quotes or double quotes.

Let’s take a first example for create javaScript string; as shown below:

let str = 'Hi';
let str2 = "Martin";
console.log(str +" "+  str2)

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

Let’s take second example:

let name = 'Martin';
let message = `Hello ${name}`;
console.log(message); // Hello Martin

In the above example; a string message evaluates the name variable in a script and returns the result string.

Remove special characters in a string javascript

The solution to avoid that problem is to use a backslash escape character.

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

The following example uses the backslash character to avoid the double-quote character in a string:

let str = "Hello World, I am \"string\" in js";
console.log(str) // Hello World, I am "string" in js

To use \ with the single quote in a string.

Let’s take a example to remove special characters from string in javaScript; as shown below:

let str = 'It\'s JavaScript Ex.';
console.log(str); //  It's JavaScript Ex.

How to get the length of the string In javaScript

The length property returns the length of a string in javaScript: Let’s see the following example:

let str = "Have a nice day!";
console.log(str.length);  // 16

How to Get Character in String JavaScript

To get the characters from string without using any built-in function in javascript, you can use array [] notation and specifying the characters of index in string, which you want to access from it.

The following example gets the first character of string javaScript:

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

To get last character of string in javascript, you use the length - 1 index.

See the following example:

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

How to Concatenation String in Javascript Using + Operator

In javascript, concatenate or join two or more strings, you use the + operator.

See the following example:

let name = 'John';
let str = 'Hello ' + name;
console.log(str); // "Hello John"

Concatenate multiple strings javascript.

See the following example:

let str = 'Hello';
str += ' This is'
str += ' Javascript';
console.log(str);  // Hello This is Javascript

How to convert Any Data type to String in JavaScript

If you want to convert non-string data type (number, boolean, etc) to a string in javascript, you can use JS toString() method.

See the following example:

Note:- The JS toString() method doesn’t work with undefined and null.

let num = 123;
console.log(typeof(num)); // number
let str = num.toString(); 
console.log(typeof(str)); // string

JavaScript String Split to Array

To split string by comma, space, any delimiter in javaScript, You can use javascript split() method.

Let’s take an example for split string by comma, space and any delimiter in JavaScript; as shown below:

var str = "Hello, This is javascript spilt string tutorial.";
var res = str.split(" ");
console.log(res); //  ["Hello,", "This", "is", "javascript", "spilt", "string", "tutorial."]

How to Compare Strings in JavaScript

If you want to compare two strings in javascript, you can use the following operators:

  • > Greater
  • >= Greater than equal to
  • < Less
  • <= Less than equal to
  • ==  Equal to

These operators compare strings based on numeric values ​​of JavaScript characters; as shown below:

let result = 'a' < 'b';
console.log(result); // true

However:

let result = 'a' < 'B';
console.log(result); // false

Conclusion

In this tutorial, you have learned everything about javascript strings.

Recommended JavaScript Tutorials

Leave a Comment