JS Tutorial



JS STRINGS


JavaScript Strings

In JavaScript, a string is a data type used to represent text rather than numbers or other types of values. Strings are enclosed in either single quotes (' '), double quotes (" "), or backticks (` `).

πŸ”Ή Key Concepts of Strings:
  • String Declaration: Strings are declared using single, double, or backtick quotes.
  • String Length: The length property of a string tells you how many characters are in it.
  • String Concatenation: Joining multiple strings into one string.
  • String Methods: JavaScript provides various methods to manipulate and handle strings.

πŸ“Œ Declaring Strings

Strings in JavaScript can be declared in three ways:

1. Single Quotes

let message = 'Hello, world!';

2. Double Quotes

let message = "Hello, world!";

3. Template Literals (Backticks)

let message = `Hello, world!`;

πŸ“Œ Accessing String Length

You can find the number of characters in a string using the length property.

Syntax:

let length = string.length;

Example:

let message = "Hello, world!";
console.log(message.length); // Output: 13

πŸ“Œ String Concatenation

String concatenation is the process of combining two or more strings together.

Syntax:

let combinedString = string1 + string2;

Example:

let greeting = "Hello";
let name = "Alice";
let message = greeting + ", " + name + "!";
console.log(message); // Output: Hello, Alice!

πŸ“Œ String Methods

JavaScript provides several built-in methods that allow you to manipulate strings. Here are some commonly used ones:

1. toUpperCase()

This method converts the entire string to uppercase.

let upperCaseString = string.toUpperCase();

Example:

let message = "hello";
console.log(message.toUpperCase()); // Output: HELLO

2. toLowerCase()

This method converts the entire string to lowercase.

let lowerCaseString = string.toLowerCase();

Example:

let message = "HELLO";
console.log(message.toLowerCase()); // Output: hello

3. slice()

This method extracts a part of a string, based on the given start and end index.

let slicedString = string.slice(startIndex, endIndex);

Example:

let message = "Hello, world!";
console.log(message.slice(0, 5)); // Output: Hello

4. replace()

This method replaces a substring with another string.

let newString = string.replace(oldSubstring, newSubstring);

Example:

let message = "Hello, world!";
console.log(message.replace("world", "Alice")); // Output: Hello, Alice!

πŸ“Œ String Escape Sequences

In JavaScript, escape sequences are used to insert special characters in strings, such as newline characters, tab characters, etc.

Common Escape Sequences:

  • \n – Newline
  • \t – Tab
  • \\ – Backslash
  • \' – Single quote
  • \" – Double quote

Example:

let message = "Hello,\nworld!";
console.log(message); // Output: Hello,
                      //         world!

πŸ“Œ Summary of JavaScript Strings:

  • Strings can be declared using single quotes, double quotes, or backticks (template literals).
  • String properties and methods allow you to manipulate text, such as converting to uppercase, slicing, replacing text, etc.
  • Escape sequences are used for inserting special characters within a string.

πŸ“Œ Practice Exercise: Work with Strings

Try working with strings and applying methods. Here’s a simple task:


Your result will appear here

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review