JavaScript Strings are a data type used to represent text. They are a sequence of characters and can be represented by enclosing the text within single quotes ('
) or double quotes ("
). For example:
let str1 = 'Hello World!';
let str2 = "Hello World!";
Both str1
and str2
are valid strings in JavaScript and can be used interchangeably.
String Concatenation:
JavaScript provides several methods for manipulating strings, such as concat()
. The concat()
method allows you to combine or concatenate two or more strings into a single string. For example:
let str1 = "Hello";
let str2 = "World";
let str3 = str1.concat(" ", str2);
console.log(str3); // Output: "Hello World"
Accessing individual characters:
You can access individual characters in a string using bracket notation. Bracket notation uses an index value to access a specific character in the string. The index value starts from 0
and goes up to length - 1
. For example:
let str = "Hello World!";
console.log(str[0]); // Output: "H"
String Properties and Methods:
JavaScript provides several properties and methods for obtaining information about strings, such as length
, toUpperCase()
, and toLowerCase()
. The length
property returns the number of characters in a string, while the toUpperCase()
and toLowerCase()
methods convert the string to all uppercase or all lowercase characters, respectively. For example:
let str = "Hello World!";
console.log(str.length); // Output: 12
console.log(str.toUpperCase()); // Output: "HELLO WORLD!"
console.log(str.toLowerCase()); // Output: "hello world!"
String Templates: