Javascript String

Introduction

In JavaScript, a string is a sequence of characters used to represent text. Strings are one of the fundamental data types in JavaScript and are commonly used to store and manipulate textual data.

String can be enclosed in Single Quotes like ‘john’ or Double Quotes like “john” or backtics (which came into ES6) like `john`.

What is a JavaScript String?

A String in JavaScript is a sequence of characters enclosed inside quotes. It can contain letters, numbers, symbols, and spaces.

JavaScript supports three ways to create strings:

  1. Single quotes (”)
  2. Double quotes (“”)
  3. Template literals ( )

Syntax of JavaScript String


let variableName = 'value';
//or
let variableName = "value";
//or
let variableName = `value`;

Example:


let name = 'John';
let city = "Delhi";
let message = `Welcome to JavaScript`;

Creating Strings in JavaScript

You can create a string in the following ways.

1. Using Double Quotes


let name = "John";
console.log(name);

Output:

John

2. Using Single Quotes


let city = 'Delhi';
console.log(city);

Output:

Delhi

3. Using Template Literals

Template literals support variables and multiline strings.


let name = "John";
console.log(`Hello ${name}`);

Output:

Hello John

4. Using String() Method

You can create a string through the String() function.


var age=35;
var age_string=String(age);  
console.log(age_string);  // Output:- "35"

Output:

“35”

5.  Using toString() Method

toString() is used to convert data into a string.


var age =35;
var age_string=age.toString(); 
console.log(age_string);  // Output:- "35"

Output:

“35”

6. Using a new keyword

Creating a String Object through the new keyword, and it behaves like an Object.


var string_object = new String("John");
console.log(string_object);         
console.log(string_object.toString()); 
console.log(string_object.valueOf()); 

Output:

[String: ‘John’]
John
John

JavaScript String Methods

Strings provide built-in methods for manipulating text.

1. length Property

It returns the total number of characters.


let text = "John";
console.log(text.length);

Output:

4

2. toUpperCase() Method

It converts text into uppercase.


let name = "john";
console.log(name.toUpperCase());

Output:

JOHN

3. toLowerCase() Method

It converts text into lowercase.


let city = "DELHI";
console.log(city.toLowerCase());

Output:

delhi

4. trim() Method

It removes extra spaces.


let str = "   Hello   ";
console.log(str.trim());

Output:

Hello

5. includes() Method

It checks whether text exists.


let msg = "Learn JavaScript";
console.log(msg.includes("Java"));

Output:

true

6. replace() Method

It replaces characters or words.


let text = "I like Java";
console.log(text.replace("Java","JavaScript"));

Output:

I like JavaScript

7. slice() Method

It extracts part of a string.


let word = "Hello World";
console.log(word.slice(0,4));

Output:

Hell

8. split() Method

It converts a string into an array.


let fruits = "Apple,Banana,Mango";
console.log(fruits.split(","));

Output:

[“Apple”,”Banana”,”Mango”]

How to Concatenate a String

Combining multiple strings is called concatenation.

1. Using + to add Multiple Strings

Example:


let firstName = "John";
let lastName = "Taylor";
console.log(firstName + " " + lastName);

Output:

John Taylor

2. Using template literals:


let first = "John";
let last = "Taylor";
console.log(`${first} ${last}`);

Output:

John Taylor

Escape Characters in Strings

Escape characters allow special formatting.

Example:


let text = "He said \"Hello\"";
console.log(text);

Output:

He said “Hello”

Common escape characters:

Escape Character Meaning Example Output
\n New Line Moves text to next line
\t Tab Adds horizontal space
\" Double Quote Prints ” inside string
\' Single Quote Prints ‘ inside string
\\ Backslash Prints \

Example of JavaScript String

Example 1: Display User Name


let username = "John";
console.log("Welcome " + username);

Output:

Welcome John

Example 2: Find String Length


let course = "JavaScript";
console.log(course.length);

Output:

10

Example 3: Convert Uppercase


let name = "John";
console.log(name.toUpperCase());

Output:

JOHN

Common Mistakes in JavaScript Strings

You will see some common mistakes in JavaScript Strings

Mistake 1: Forgetting Quotes

Wrong:


let name = John;

Error:

ReferenceError

Correct:


let name = "John";

Mistake 2: Using the Wrong Case in Methods

Wrong:


text.touppercase();

Correct:


text.toUpperCase();

Note: JavaScript is case-sensitive.

Mistake 3: Mixing Quotes Incorrectly

Wrong:


let text = "It's good";

Correct:


let text = "It's good";

or


let text = 'It\'s good';

Mistake 4: Confusing Number and String


let age = "20";
console.log(age + 5);

Output:

205

Note: Because “20” is a string.

Correct:


console.log(Number(age)+5);

Output:

25

JavaScript String – Interview Questions

Q 1: What is a string?
Ans: A sequence of characters.
Q 2: How to define a string?
Ans: Using single, double quotes, or backticks.
Q 3: Are strings immutable?
Ans: Yes.
Q 4: How to find the string length?
Ans: Using .length.
Q 5: How to convert to uppercase?
Ans: toUpperCase().

JavaScript String – Objective Questions (MCQs)

Q1. Strings in JavaScript are written inside ______.






Q2. Which method returns string length?






Q3. Which is a valid string?






Q4. Which method converts string to uppercase?






Q5. JavaScript strings are ______.






Conclusion

JavaScript strings are used to store and manipulate text data. They are essential in web development because websites constantly work with names, messages, form inputs, API responses, and dynamic content. Learning string methods like length(), slice(), replace(), split(), trim(), and includes() helps developers write efficient JavaScript code.

If you want to become good at JavaScript, understanding strings is one of the most important topics to master because almost every real-world project uses them.