# Strings In JavaScript, strings are a fundamental data type used to represent text. Strings are sequences of characters, and they are often used for storing and manipulating textual data. JavaScript provides a variety of methods and operations for working with strings. Here's an overview of strings in JavaScript: ## Creating Strings You can create strings in JavaScript using single quotes (''), double quotes ("") or backticks (``). Single and double quotes are used to create simple strings, while backticks are used for template literals, which allow for string interpolation. ```javascript let singleQuoted = 'This is a single-quoted string.'; let doubleQuoted = "This is a double-quoted string."; let templateLiteral = `This is a template literal string.`; ``` ## String Methods JavaScript provides a wide range of methods to manipulate and work with strings. Here are some common string methods: ### 1. Concatenation You can concatenate (join) strings together using the `+` operator or the `concat()` method: ```javascript let firstName = "John"; let lastName = "Doe"; let fullName = firstName + " " + lastName; // Using the + operator let greeting = firstName.concat(" ", lastName); // Using the concat() method ``` ### 2. String Length You can get the length of a string using the `length` property: ```javascript let message = "Hello, World!"; let length = message.length; // length is 13 ``` ### 3. Accessing Characters You can access individual characters in a string by their index: ```javascript let text = "JavaScript"; let firstChar = text[0]; // "J" let fifthChar = text.charAt(4); // "S" ``` ### 4. Substrings You can extract substrings from a string using the `substring()` or `slice()` methods: ```javascript let sentence = "This is a sample sentence."; let substring = sentence.substring(5, 12); // "is a sa" let slicedSubstring = sentence.slice(5, 12); // "is a sa" ``` ### 5. Searching You can search for substrings within a string using `indexOf()` and `lastIndexOf()`, or you can use `includes()`, `startsWith()`, and `endsWith()`: ```javascript let sentence = "This is a sample sentence."; let indexOfA = sentence.indexOf("a"); // 8 let includesSample = sentence.includes("sample"); // true let startsWithThis = sentence.startsWith("This"); // true let endsWithDot = sentence.endsWith("."); // true ``` ### 6. Case Conversion You can convert the case of a string using `toUpperCase()` and `toLowerCase()`: ```javascript let text = "Hello, World!"; let uppercaseText = text.toUpperCase(); // "HELLO, WORLD!" let lowercaseText = text.toLowerCase(); // "hello, world!" ``` ### 7. Replace and Split You can replace parts of a string or split it into an array using `replace()` and `split()`: ```javascript let sentence = "This is a sample sentence."; let replaced = sentence.replace("sample", "great"); // "This is a great sentence." let words = sentence.split(" "); // ["This", "is", "a", "sample", "sentence."] ``` These are just a few of the many string methods available in JavaScript. ## String Interpolation With template literals (backticks), you can easily interpolate variables and expressions within strings: ```javascript let name = "Alice"; let greeting = `Hello, ${name}!`; // "Hello, Alice!" ``` ## Escape Characters To include special characters in a string, you can use escape characters, such as `\n` for a new line or `\"` for a double quote within a string. ```javascript let escapedString = "This is a\nmulti-line\nstring."; let withQuotes = "She said, \"Hello!\""; ``` JavaScript provides a rich set of tools for working with strings, making it possible to manipulate, search, and modify text effectively. Understanding and using string methods is essential for handling text data in JavaScript. ## Example 1 ```js let text = "JavaScript is fun"; console.log(text); console.log(typeof(text)); ``` **Output** ``` JavaScript is fun string ``` *** ## Example 2 ```js let age = 25; let text = `I am ${age} years old`; console.log(text); console.log(typeof(text)); ``` **Output** ``` I am 25 years old string ``` *** ## Access String Characters ```js let text = "Hello"; console.log(text[0]); console.log(text[1]); console.log(text[2]); ``` **Output** ``` H e l ``` *** ## JavaScript String Length ```js let text = "Hello"; console.log(text.length); ``` **Output** ``` 5 ``` *** ## charAt() Method ```js let text = "Hello"; console.log(text.charAt(0)); console.log(text.charAt(1)); console.log(text.charAt(2)); ``` **Output** ``` H e l ``` *** ## JavaScript String is Case-Sensitive ```js let str1 = "HELLO"; let str2 = "hello"; console.log(str1 === str2); ``` **Output** ``` false ``` *** ## JavaScript Multiline Strings ```js const message = "Hello everyone. \ Welcome to our JavaScript tutorial. \ In this tutorial, you will learn about \ JavaScript in a fun and easy way."; console.log(message); ``` **Output** ``` Hello everyone. Welcome to our JavaScript tutorial. In this tutorial, you will learn about JavaScript in a fun and easy way. ``` *** ## Assignment and Task **Create a JavaScript string and find the first and last character of the string using the charAt() method. Also find the length of the string.** ### Solution: ```js const message = "Hello everyone. \ Welcome to our JavaScript tutorial. \ In this tutorial, you will learn about \ JavaScript in a fun and easy way."; // length of the string const totalLength = message.length; console.log(`Length: ${totalLength}`) // first character console.log(message.charAt(0)); //last character console.log(message.charAt(totalLength-1)); ``` **Output** ``` Length: 136 H . ``` *** ## p4n Quiz **Q. What is the output of the program?** ```js let message = "JavaScript"; let len = message.length; console.log(message.charAt(len - 2)); ``` 1. Error 2. p 3. t 4. J Answer: 2