Menu Close

JavaScript String

In this tutorial, you will learn about JavaScript string and its methods with the help of examples. In the previous tutorial, we have seen all about JavaScript date objects and their methods with the help of examples.

In this guide, we will give a complete package of JavaScript strings and their usages using some appropriate examples.

JavaScript String

In javascript, the string is a collection of characters, which is used for storing and manipulating the text. JavaScript string is always written inside single or double quotes. A JavaScript string has zero or more characters.

Example:


var name = 'JavaScript';
var lang = "JavaScript";
var x = ' ';
var y = " ";

As you can see in above explain, we have some JavaScript variables with single and double quotes and also empty string.

Create JavaScript String

In JavaScript, There are three ways to create JavaScript strings.

  • Using single quotes.
  • Using double quotes.
  • Using backticks.

Example:


let name = "Programming"; // Using double quotes
let name1 = 'Funda'; // Using single quotes
let result = `${name} ${name1} is the best place to learn JavaScript`;
console.log(result); 
// Output
//Programming Funda is the best place to learn JavaScript.

Note:- JavaScript Backtick is used to store the variable value or expression inside the string.
Single quotes and double quotes are the same so you can use either of them.

Access string characters

  • There are two ways to available to access characters from the sting.

Example


let name = "Programming"; 
console.log(name[1]); // r
  • And another ways to access character from the string using string charAt() function.

Example


let name = "Programming"; 
console.log(charAt(2)); // o

JavaScript strings are immutable

JavaScript strings are immutable, That means characters of the string can not be changes once it has created.

Example


let name = "Programming"; 
name[1] = 'a';
console.log(name);

JavaScript support multi line strings

To use multi line string , you can use + operator or slash \.

Example 1:


let name = "Programming Funda " + 
"is the best place to learn coding."; 
console.log(name);

// Output
// Programming Funda is the best place to learn coding.

Example 2:


let name = 'Programming Funda  \
is the best place to learn coding.'; 
console.log(name);

// Output
// Programming Funda is the best place to learn coding.

String Length

To find the length of the string use built-in length property.

Example


let name = "Programming Funda"
console.log(name.length);

Escape Character

Sometimes we want to write a special character within a string.
for example, we want to display a message like: Welcome to “programming funda tutorial”.
If we try to do this like below then JavaScript produce an error.

Example


var name = "Welcome to "programming funda tutorial""
console.log(name);

To avoid the above problem we will use the backlash ( \ ) escape character. The backslash ( \ ) escape character turns special characters into string characters.

Example


var name = "Welcome to \"programming funda tutorial\""
console.log(name);
CodeResult
\’
\”
\\\

There are six types of escape character valid in JavaScript.

CodeResult
\bBackspace
\fForm Feed
\nNew Line
\rCarriage Return
\tHorizontal Tabulator
\tVertical Tabulator

Line Breaks:

You can break the lines in JavaScript to making a more readable.

Example


var name = "Welcome to \"programming funda tutorial\"";
console.log(name);

JavaScript String Object

In JavaScript, the string can be an object. To define a string as an object you can use a new keyword.
To check the type of string you can use typeof operator.

Example


let name1 = "Programming Funda";
let name2 = new String("Programming Funda");

console.log(typeof name1);
console.log(typeof name2);

//Output
// string
// object

When we will use ( == ) operator it will return true because both a and b has same value.

Example


var a = new String("programming Funda");
var b = "programming Funda";
console.log(a == b);

When we will use ( === ) operator, it will return false because both a and b has same value but different data type.

Example


var a = new String("programming Funda");
var b = "programming Funda";
console.log(a === b);

JavaScript String Methods

Here we mention some commonly used JavaScript string methods.

  • charAt():- return the character at the specified position.
  • concat():- Join two or more strings.
  • replace():- replace a string with another string.
  • split():- Convert the string into an array of the string.
  • substr(start, length):- return the part of the string with a specified length.
  • substring(start end):- Return a part of the string.
  • slice():- Return a part of string.
  • toLowerCase():- Return a passed string into lower case.
  • toUpperCase():- Return a passed string into upper case.
  • trim():- Remove whitespaces from the string.
  • includes():- Searches for a string and returns a boolean value.
  • search():- Searches for a string and returns the position of matches.

Example: JavaScript Strings Methods


let name1 = " Programming ";
let name2 = "Funda";

console.log(name2.charAt(1)); // u
console.log(name1.concate(name2)); // 

// Convert text to upper case
console.log(name2.toUpperCase());

// Convert text to lower case
console.log(name2.toLowerCase());

// Remove whitespace from the string.
console.log(name1.trim());

To learn more about JavaScript strings methods, click here.

Summary

In this JavaScript guide, we have seen all about JavaScript strings with the help of examples.
Strings are the most important in any programming language. If you are working with JavaScript, you can’t underestimate the strings. You can create strings in JavaScript using three ways single quotes, double quotes, and backticks.

Strings in JavaScript provide useful string methods that will be going to be very helpful when you will work with Strings in JavaScript.

I hope this article will have helped you. If you like this guide, don’t forget to share.

JavaScript Articles


Previous Article
Next Article

Was this post helpful?

Related Posts