JavaScript Strings

In javascript, strings are used to represent and manipulate textual data. They are sequences of characters enclosed in single quotes (`'`) or double quotes (`"`). Here are some basic operations and concepts related to working with strings in javascript.


1. Creating a string: you can create a string by enclosing characters within quotes:

javascript
const message = 'hello, world!';
const name = "john doe";

both single quotes and double quotes can be used interchangeably to define a string. Additionally, you can use backticks (`` ` ``) to create template literals, which allow for easier string interpolation and multiline strings.


2. String length: you can determine the length of a string using the `length` property:

javascript
const message = 'hello, world!';
console.log(message.length); // output: 13

3. String concatenation: you can concatenate (join) strings using the `+` operator:

javascript
const firstname = 'john';
const lastname = 'doe';
const fullname = firstname + ' ' + lastname;
console.log(fullname); // output: 'john doe'

another way to concatenate strings is by using template literals:

javascript
const firstname = 'john';
const lastname = 'doe';
const fullname = `${firstname} ${lastname}`;
console.log(fullname); // output: 'john doe'

4. Accessing characters: you can access individual characters within a string using square brackets `[]` and the zero-based index:

javascript
const message = 'hello, world!';
console.log(message[0]); // output: 'h'
console.log(message[7]); // output: 'w'

note that javascript strings are immutable, meaning that you cannot modify individual characters directly.


5. String methods: javascript provides various built-in methods to manipulate and work with strings. Some common string methods include:

- `tolowercase()`, `touppercase()`: convert the string to lowercase or uppercase.
- `trim()`: remove leading and trailing whitespace from the string.
- `split(separator)`: split the string into an array of substrings based on a specified separator.
- `substring(startindex, endindex)`: extract a portion of the string between the specified indices.
- `indexof(searchvalue)`, `lastindexof(searchvalue)`: find the index of the first or last occurrence of a substring within the string.
- `replace(searchvalue, replacement)`: replace occurrences of a substring with another substring.


example:

javascript
const message = ' hello, world! ';
Console.log(message.tolowercase()); // output: ' hello, world! '
Console.log(message.trim()); // output: 'hello, world!'
console.log(message.split(' ')); // output: ['hello,', 'world!']
console.log(message.indexof('o')); // output: 4
console.log(message.replace('world', 'universe')); // output: ' hello, universe!

6. String comparison: you can compare strings using relational operators (`<`, `>`, `<=`, `>=`). Javascript performs lexicographic (dictionary) comparison, comparing characters based on their unicode values.

javascript
const str1 = 'apple';
const str2 = 'banana';

console.log(str1 < str2); // output: true
console.log(str1 > str2); // output: false

note that the comparison is case-sensitive.


These are some of the basic operations and concepts related to working with strings in javascript.



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext