JavaScript String

JavaScript String

In this tutorial, you will learn everything about the JavaScript string.

JavaScript Strings

In JavaScript, a string is a sequence of characters. Which is written within single or double quotations. For example, both “Hello JavaScript” and ‘KL55HN’ are examples of strings

JavaScript strings are also immutable. It means the original string doesn’t change.

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

See the following example:

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

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

See the following example:

let name = 'Martin';
let msg = `Hello ${name}`;

console.log(msg ); // Hello Martin

Here, The string msg evaluates the name variable in a script and returns the result string.

Ignore special characters

let str = "Hello World, I am "string" in js";

Strings are written within quotes as mention above,  In this example, Script through exception like this:

JavaScript error: Uncaught SyntaxError: Unexpected identifier on line 2

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.

See the following example

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

Getting the length of the string In javaScript

The length property returns the length of a string:

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

JavaScript Access Character in String

To access 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"

String Concatenation 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

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

If you want to split string by comma, space, any delimiter in javascript, You can use javascript split() method.

See the following example:

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

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.

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.

AuthorTuts Make

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Javascript, JQuery, Laravel, Livewire, Codeigniter, Vue JS, Angular JS, React Js, WordPress, and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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