How to declare strings in JavaScript?
In JavaScript, you can declare strings using the "string" data type. Here are some examples:
// Declaration of strings
let str1 = "Hello, world!"; // using double quotes
let str2 = 'JavaScript is awesome'; // using single quotes
let str3 = `The result is ${num1 + num2}`; // using backticks for template literals
// String concatenation
let fullName = "John" + " " + "Doe"; // concatenating two strings
let greeting = "Hello, " + fullName; // concatenating string with a variable
// String methods
let sentence = "The quick brown fox jumps over the lazy dog.";
let length = sentence.length; // get the length of a string
let upperCase = sentence.toUpperCase(); // convert the string to uppercase
let lowerCase = sentence.toLowerCase(); // convert the string to lowercase
let subString = sentence.substring(4, 19); // get a substring
let split = sentence.split(" "); // split the string into an array of substrings
console.log(str1); // Output: Hello, world!
console.log(str2); // Output: JavaScript is awesome
console.log(str3); // Output: The result is 13 + 14
In the examples above, we declare three strings using different ways of quotes. We then perform string concatenation using the "+" operator and demonstrate some commonly used string methods such as "length", "toUpperCase()", "toLowerCase()", "substring()", and "split()". These methods allow us to manipulate and work with string data in various ways.