Strings

Introduction

Strings in JavaScript are used to store and work with text. They are one of the most commonly used data types in web development and are essential for handling user input, displaying content, and processing data.

Understanding Strings

A string is a sequence of characters enclosed in single quotes, double quotes, or backticks. It can include letters, numbers, symbols, and spaces.

Example
let name = “Ali”
let message = ‘Welcome to JavaScript’
let greeting = Hello User

Creating Strings

You can create strings using different types of quotes. Backticks are especially useful because they allow multi line text and variable embedding.

Example
let text = This is a string

String Length

The length property returns the number of characters in a string.

Example
let word = “JavaScript”
console.log(word.length)

Common String Methods

JavaScript provides built in methods to manipulate strings.

toUpperCase converts text to uppercase
toLowerCase converts text to lowercase
trim removes extra spaces
slice extracts part of a string
replace replaces part of a string

Example
let text = ” hello world “
console.log(text.trim())
console.log(text.toUpperCase())

String Concatenation

Strings can be combined using the plus operator or template literals.

Example
let firstName = “Ali”
let lastName = “Khan”
let fullName = firstName + ” ” + lastName

Template Literals

Template literals allow you to embed variables directly into strings using backticks.

Example
let name = “Sara”
let message = Hello ${name}

Escape Characters

Escape characters allow you to include special symbols inside strings.

Example
let quote = “He said “Hello””

Common Uses of Strings

Displaying messages on websites
Handling form inputs
Storing and processing text data
Creating dynamic content

Conclusion

Strings are a fundamental part of JavaScript. Learning how to create and manipulate strings helps you build dynamic and user friendly web applications.

Home ยป JavaScript Fundamentals (Beginner Level) > Variables and Data Types > Strings