Sunday, 10 November 2024

JavaScript Escape Sequences Guide with Examples

In JavaScript, escape sequences are used within strings to represent special characters that are not easily typed directly or could have a special behaviour if included in a string. These sequences begin with a backslash (\) followed by a character that represents the specific special character.

Here’s a detailed look at all the common escape sequences in JavaScript:

 

1. \' - Single Quote

Represents a single quote. Useful when you have a single-quoted string and want to include a single quote inside it.

 

Example

let singleQuote = 'It\'s a sunny day!';
console.log(singleQuote); // Output: It's a sunny day!

 

2. \" - Double Quote

Represents a double quote. Useful in double-quoted strings when you want to include double quotes inside them.

 

Example

 

let doubleQuote = "He said, \"Hello!\"";
console.log(doubleQuote); // Output: He said, "Hello!"

 

3. \\ - Backslash

Represents a backslash. Used when you need to include a backslash in the string.

 

Example

 

let backslash = "This is a backslash: \\";
console.log(backslash); // Output: This is a backslash: \

 

4. \n - New Line

Inserts a new line in the string.

 

Example

 

let newLine = "First line\nSecond line";
console.log(newLine);
/* 
Output:
First line
Second line
*/

 

5. \r - Carriage Return

Inserts a carriage return. It moves the cursor to the beginning of the line but doesn't move it to the next line. It's often used with \n (newline) for legacy systems.

 

Example

 

let carriageReturn = "Hello\rWorld!";
console.log(carriageReturn); // Output: World! (Overwrites "Hello")

6. \t - Tab

Inserts a horizontal tab, which moves the cursor to the next tab stop.

 

Example

let tab = "Column1\tColumn2";
console.log(tab); // Output: Column1    Column2

7. \b - Backspace

Moves the cursor one position back (deletes one character). It’s not widely used and might not be supported in some browsers.

 

Example

 

let backspace = "abc\bdef";
console.log(backspace); // Output: abdef

 

8. \f - Form Feed

Inserts a form feed. This was used in printing to move to the next page and is rarely used today.

 

Example

 

let formFeed = "Hello\fWorld";
console.log(formFeed); // Output: Hello (new page) World (Effect depends on the environment)

 

9. \v - Vertical Tab

Inserts a vertical tab. Similar to \n, but instead of moving to the next line, it moves the cursor down vertically.

 

Example

 

let verticalTab = "First\vSecond";
console.log(verticalTab); // Output: First (vertical tab) Second (Effect varies)

 

10. \0 - Null Character

Inserts a null character, which has a character code of 0. It doesn’t terminate the string but might be used for specific purposes.

 

Example

 

let nullChar = "Hello\0World";
console.log(nullChar); // Output: HelloWorld (No visible effect)

 

11. \uXXXX - Unicode

Inserts a Unicode character, where XXXX is the hexadecimal code point.

 

Example

 

let unicode = "Unicode character: \u263A";
console.log(unicode); // Output: Unicode character: ☺

 

12. \xXX - Hexadecimal

Inserts a character based on its hexadecimal value. Similar to \u but uses only two hexadecimal digits.

 

Example

 

let hex = "Hexadecimal: \x41"; // Hexadecimal for 'A'
console.log(hex); // Output: Hexadecimal: A

 

13. \ - Backtick (in template literals)

In template literals, if you want to include a backtick, you escape it using \.

 

Example

 

let templateLiteral = `This is a \`backtick\` inside a template literal.`;
console.log(templateLiteral); // Output: This is a `backtick` inside a template literal.

 

escapeSequences.js

let singleQuote = 'It\'s a sunny day!';
console.log(singleQuote); // Output: It's a sunny day!

let doubleQuote = "He said, \"Hello!\"";
console.log(doubleQuote); // Output: He said, "Hello!"

let backslash = "This is a backslash: \\";
console.log(backslash); // Output: This is a backslash: \

let newLine = "First line\nSecond line";
console.log(newLine);
/* 
Output:
First line
Second line
*/

let carriageReturn = "Hello\rWorld!";
console.log(carriageReturn); // Output: World! (Overwrites "Hello")

let tab = "Column1\tColumn2";
console.log(tab); // Output: Column1    Column2

let backspace = "abc\bdef";
console.log(backspace); // Output: abdef

let formFeed = "Hello\fWorld";
console.log(formFeed); // Output: Hello (new page) World (Effect depends on the environment)

let verticalTab = "First\vSecond";
console.log(verticalTab); // Output: First (vertical tab) Second (Effect varies)

let nullChar = "Hello\0World";
console.log(nullChar); // Output: HelloWorld (No visible effect)

let unicode = "Unicode character: \u263A";
console.log(unicode); // Output: Unicode character: ☺

let hex = "Hexadecimal: \x41"; // Hexadecimal for 'A'
console.log(hex); // Output: Hexadecimal: A

let templateLiteral = `This is a \`backtick\` inside a template literal.`;
console.log(templateLiteral); // Output: This is a `backtick` inside a template literal.

 

Output

It's a sunny day!
He said, "Hello!"
This is a backslash: \
First line
Second line
World!
Column1	Column2
abdef
Hello
     World
First
     Second
HelloWorld
Unicode character: ☺
Hexadecimal: A
This is a `backtick` inside a template literal.

 

Escape sequences are essential for handling special characters, formatting text. Understanding these escape sequences allows for greater flexibility in working with strings in JavaScript.

 


Previous                                                    Next                                                    Home

No comments:

Post a Comment