JavaScript for Beginners
About Lesson

String is a sequence of characters. In JavaScript, strings are enclosed within quotes. You can use single quotes ('), double quotes ("), or backticks (“`).

let str1 = 'Hello, world!';
let str2 = "Hello, world!";
let str3 = `Hello, world!`;


String Properties and Methods

JavaScript strings have many built-in properties and methods. For example, the length property returns the length of a string and the toUpperCase() method converts a string to uppercase letters.

let str = "Hello, world!";
console.log(str.length); // Outputs: 13

let upperStr = str.toUpperCase();
console.log(upperStr); // Outputs: HELLO, WORLD!


Special Characters in Strings

In JavaScript strings, you can use special characters by using a backslash () followed by the character you want to insert.

let str = "We are the so-called "Vikings" from the north.";
console.log(str); // Outputs: We are the so-called "Vikings" from the north.


Template Literals

Template literals are a way to output variables in the string. They are defined using backticks and ${} to embed variables.

let name = "Junaid";
let str = `Hello, ${name}!`;
console.log(str); // Outputs: Hello, Junaid!


Strings are a powerful tool in JavaScript, allowing you to manipulate and interact with textual data. They are a fundamental concept that’s widely used in programming, so understanding how they work will greatly enhance your JavaScript coding skills.