Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
Remove a Character From String in JavaScript
Next article icon

What are Escape Characters in JavaScript ?

Last Updated : 07 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Escape Characters(Backslash) are the symbol used to begin an escape command in order to execute some operation. Escape characters are characters that are used when working with special characters like single quotes, and double quotes, To print these characters as it is, including the backslash ‘\’ in front of them.

Syntax:

For single quotes:  \’  (backslash followed by single quote)  For double quotes: \”  (backslash followed by double quotes)

List of escape characters available in javascript:

Escape characterResult
\b This character is used to give a backspace
\f This character is used for form feed
\n This character is used to give a new Line
\r This character is used for carriage Return
\t This character is used for horizontal Tabulator
\v This character is used for vertical Tabulator
 

Example 1: In this example, we are using single quotes \' and double  quotes\"

JavaScript
let str = "Geeks \'for\' Geeks"; console.log(str); let str2 = "Geeks \"for\" Geeks"; console.log(str2); 

Output:

Geeks 'for' Geeks  Geeks "for" Geeks

Example 2: In this example, we will use backlash in javascript.

HTML
<!DOCTYPE html> <html>  <body>     <h2>Escape character in Javascript</h2>      <p id="result"></p>      <script>         let text = `This example shows the use of              a backslash character in Javascript.              The character \\ is called backslash.`;          document.getElementById("result").innerHTML = text;      </script> </body>  </html> 

Output:

 

Example 3: Here by using backslash we will break our string in javascript.

HTML
<!DOCTYPE html> <html>  <body>     <h1>Escape characters in Javascript</h1>      <h2>Breaking the string using backslash</h2>      <p id="result"></p>      <script>         document.getElementById("result").innerHTML              = "Hello \world! \from GEEKSFROMGEEKS";     </script> </body>  </html> 

Output:

 

Next Article
Remove a Character From String in JavaScript

G

geekwriter2024
Improve
Article Tags :
  • JavaScript
  • Web Technologies

Similar Reads

  • What are the encodeURI() and decodeURI() in JavaScript ?
    URLs and URIs are designed to locate/identify resources available over the internet, anything that uniquely identifies a resource is its URI, such as id, name. A URL specifies a resource and its access protocol. All URLs are URIs, but not all URIs are URLs. URI can only have certain characters from
    3 min read
  • Remove a Character From String in JavaScript
    In JavaScript, a string is a group of characters. Strings are commonly used to store and manipulate text data in JavaScript programs, and removing certain characters is often needed for tasks like: Removing unwanted symbols or spaces.Keeping only the necessary characters.Formatting the text.Methods
    3 min read
  • Get Unicode Character Value in JavaScript
    Here are various ways to get Unicode character values in JavaScript 1. Using charCodeAt() to Get Unicode ValuesThis code defines a string letter containing the character "A". It then uses the charCodeAt() method to get the Unicode value of the first character (index 0) and logs the result, which is
    5 min read
  • Which Characters Should Be Escaped Inside A "pre" tag?
    The <pre> tag defines pre-formatted text. Everything (generally, text and code snippets) in a <pre> tag element is displayed in a fixed-width font, and preserves both spaces and line breaks in them. In other words, if anyone wants to show their code snippets on a web page, they can simpl
    3 min read
  • How to Create an Alert in JavaScript ?
    The alert() method in JavaScript displays an alert box with a message and an OK button. It's used when you want information to come through to the user, providing immediate notifications or prompts for user interaction during program execution. Note: Alert boxes interrupt user interaction, shifting
    1 min read
  • How to escape & unescape HTML characters in string in JavaScript?
    Escaping and unescaping HTML characters is important in JavaScript because it ensures proper rendering of content, preventing HTML injection attacks and preserving text formatting when displaying user-generated or dynamic content on web pages. Escape HTML Characters< : &lt;> : &gt;" :
    3 min read
  • JavaScript - Insert Character in JS String
    In JavaScript, characters can be inserted at the beginning, end, or any specific position in a string. JavaScript provides several methods to perform these operations efficiently. At the BeginningTo insert a character at the beginning of a string, you can use string concatenation or template literal
    2 min read
  • How to Escape Characters in XML ?
    Escaping characters in XML is important because it ensures that special characters like <, >, &, and ", which have special meanings in XML, are properly encoded as entities like &lt;, &gt;, &amp;, &quot;, respectively. There are several approaches to escape characters in XM
    2 min read
  • JavaScript - Add Characters to a String
    Here are the various approaches to add characters to a string in JavaScript Using the + Operator - Most PopularThe + operator is the simplest way to add characters to a string. It concatenates one or more strings or characters without modifying the original string. [GFGTABS] JavaScript const s1 =
    2 min read
  • What characters are valid for JavaScript variable names ?
    In this article, we will see the valid variable names in JavaScript. The valid variable names can contain letters (both uppercase and lowercase), numbers, underscores, and dollar signs. However, the variable names can not begin with numbers. Some examples of valid variable names: var Geeks; var Geek
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences