Ticker

6/recent/ticker-posts

JavaScript - String

JavaScript - String

Introduction In JavaScript, a string is a sequence of characters enclosed within single quotes ('') or double quotes (""). Strings are used to store and manipulate text-based data. This documentation provides an overview of common operations and methods used to work with strings in JavaScript.

Creating a String Strings can be created in JavaScript using the string literal or the String constructor.

  • String Literal:
javascript
let message = 'Hello, World!';
  • String Constructor:
javascript
let message = new String('Hello, World!');

Accessing Characters Individual characters within a string can be accessed using square brackets [] and the character's index position. Note that string indices start at 0.

javascript
let message = 'Hello, World!'; console.log(message[0]); // Output: 'H'

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

javascript
let message = 'Hello, World!'; console.log(message.length); // Output: 13

Concatenation Strings can be concatenated (joined) using the plus (+) operator or the concat() method.

  • Using the plus operator:
javascript
let greeting = 'Hello'; let name = 'John'; let message = greeting + ', ' + name + '!'; console.log(message); // Output: 'Hello, John!'
  • Using the concat() method:
javascript
let greeting = 'Hello'; let name = 'John'; let message = greeting.concat(', ', name, '!'); console.log(message); // Output: 'Hello, John!'

String Methods JavaScript provides various methods to perform operations on strings. Here are some commonly used methods:

  • toUpperCase() and toLowerCase(): Converts a string to uppercase or lowercase.
  • trim(): Removes whitespace from the beginning and end of a string.
  • charAt(): Returns the character at a specified index.
  • substring(): Extracts a portion of a string between two indices.
  • indexOf(): Returns the index of the first occurrence of a specified substring.
  • replace(): Replaces a substring with another substring.
  • split(): Splits a string into an array of substrings based on a specified separator.
javascript
let message = 'Hello, World!'; console.log(message.toUpperCase()); // Output: 'HELLO, WORLD!' console.log(message.toLowerCase()); // Output: 'hello, world!' console.log(message.trim()); // Output: 'Hello, World!' console.log(message.charAt(7)); // Output: 'W' console.log(message.substring(7, 12)); // Output: 'World' console.log(message.indexOf('o')); // Output: 4 console.log(message.replace('Hello', 'Hi')); // Output: 'Hi, World!' console.log(message.split(',')); // Output: ['Hello', ' World!']

String Template Literals Template literals provide an elegant way to embed expressions within strings, allowing variables and expressions to be evaluated and interpolated.

javascript
let name = 'John'; let age = 30; let message = `My name is ${name} and I am ${age} years old.`; console.log(message); // Output: 'My name is John and I am 30 years old.'

These are just some of the basic concepts and methods related to strings in JavaScript. Strings are a fundamental part of JavaScript, and understanding how to work with them is essential for any JavaScript developer.

Post a Comment

0 Comments