JavaScript Data Type: String
1 Introduction
In JavaScript, a String is a primitive data type used to represent textual data. It
consists of a sequence of characters, such as letters, numbers, or symbols, enclosed in
single quotes (’), double quotes ("), or backticks (‘) for template literals. Strings are
immutable, meaning their content cannot be changed directly, and they support various
methods for manipulation.
2 Characteristics
• Immutability: Individual characters in a string cannot be modified; operations
create new strings.
• Quotes: Can be defined using single quotes, double quotes, or backticks (for tem-
plate literals with embedded expressions).
• Methods: Common methods include length, toUpperCase(), slice(), indexOf(),
and replace().
• Template Literals: Allow dynamic string creation with expressions using ${}.
3 Use Cases
Strings are used for:
• Storing text like names, messages, or file paths.
• Displaying user interfaces or console output.
• Manipulating text in applications, such as formatting or parsing data.
4 Example
Below is a JavaScript code example demonstrating the String data type:
1 let greeting = " Hello , World !"; // Double quotes
2 let name = ’ Alice ’; // Single quotes
3 let dynamic = ‘ Welcome , $ { name }! ‘; // Template literal
4 console . log ( greeting ) ; // Output : Hello , World !
5 console . log ( name . length ) ; // Output : 5
1
6 console . log ( dynamic ) ; // Output : Welcome , Alice !
7 console . log ( greeting . toUpperCase () ) ; // Output : HELLO , WORLD !
5 Notes
• Strings can be concatenated using the II+ operator or template literals for cleaner
syntax.
• Avoid modifying strings in a loop for performance; use arrays instead and join them.
• Template literals are preferred for dynamic strings with variables.