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:
JavaScript encodeURI(), decodeURI() and its components Functions
Next article icon

What are the encodeURI() and decodeURI() in JavaScript ?

Last Updated : 19 Oct, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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 the standard 128 ASCII character set. Reserved characters that do not belong to this set must be encoded. This means that we need to encode these characters when passing into a URL. In JavaScript, we have two special functions to serve these tasks of encoding and decoding the URIs. encodeURI() and decodeURI() these functions are introduced to encode/decode non-English characters, such as Latin, Greek letters used in URI (Uniform Resource Identifier). Also, it is helpful to encode special characters, replace whitespaces in URI; sometimes these special characters or whitespaces can delimit the URI.

1. encodeURI():  It is used to encode a given URI into UTF-8 format. The encodeURI() function takes URI (of type string) as a function parameter value and encodes a URI by replacing each instance of certain characters with one, two, three, or four escape sequences representing the UTF-8 encoding of the character.

Syntax:

encodeURI(URI)

Parameters: here, URI is the parameter that consists of a complete URI- Uniform Resource Identifier (of type string) which you want to encoded.

Return value: Above function will return a new string representing the 'encoded URI' for the given URI provided as the parameter to the function.

Note: In many browsers encodeURI() doesn't encode many characters following are the set of special characters  [ ~!@#$&*()=:/,;?+' ]  to encode these characters sets escape() is implemented separately. As well as English alphabets and numbers [ A-Z a-z 0-9 - _ . ! ~ * ' ( ) ] are not escaped by the encodeURI() for encoding complete URI string you can use encodeURI() and if you have for the part of URI string then you can use encodeURIComponent() to encode particular part of URI string.

2. decodeURI():  It is used to decode already previously encoded URI. This works in reverse, taking an encoded string and replacing the tokens with the normal characters. The decodeURI() function takes encodedURI (of type string) as a function parameter value and decodes a given encoded-URI, previously created by encodeURI() or by a similar routine. 

Syntax:

decodeURI(encodedURI)

Parameters: Here, encodedURI is the parameter that represents a complete, encoded URI (Uniform Resource Identifier). If you want decode the URI parameter must contain the URI in the encoded form only. Also, it throws an 'URIError' exception if the given parameter 'encodedURI' contains invalid character sequences.

Return value: above function will return a new string representing the decoded version of the given URI in the encoded form.

Note: Replaces each escape sequence in the encoded URI with the character that it represents, but does not decode escape sequences that could not have been introduced by encodeURI. It is used to decode the Cyrillic URLs as well; Cyrillic URL contains Cyrillic alphabet that looks similar to letters in the Latin alphabet (used in English), sometimes it is used to track the user and redirects them to fake websites.

Example:

JavaScript
<script>     const uri = 'https://www.geeksforgeeks.org/?x=γεια';     const encoded = encodeURI(uri);     console.log("Encoded URI - ", encoded);      // Expected output:      //"https://www.geeksforgeeks.org/?x=%CE%B3%CE%B5%CE%B9%CE%B1"      try {         console.log("Decoded URI - ", decodeURI(encoded));          // Expected output:          // "https://www.geeksforgeeks.org/?x=γεια"     } catch (e) {         console.error(e);     } </script> 

Output:

output
Output of encodeURI and decodeURI

Next Article
JavaScript encodeURI(), decodeURI() and its components Functions

N

npg
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • TrueGeek
  • TrueGeek-2021
  • JavaScript-Methods
  • JavaScript-Questions

Similar Reads

  • JavaScript encodeURI(), decodeURI() and its components Functions
    The encodeURI() and decodeURI() functions in JavaScript are used to handle URI (Uniform Resource Identifier) encoding and decoding. They ensure that URIs are properly formatted for web usage, converting characters that may cause issues into a valid, encoded format. 1. encodeURI() FunctionThe encodeU
    3 min read
  • How to Encode and Decode a URL in JavaScript?
    Encoding and decoding URLs in JavaScript is essential for web development, especially when making GET requests with query parameters. This process ensures special characters in URLs are correctly interpreted by the server. For instance, spaces are converted to %20 or + in URLs. This guide covers how
    5 min read
  • JavaScript | Encode/Decode a string to Base64
    To encode or decode strings in JavaScript, we can use the built-in functions provided by the language. These functions help in encoding special characters in a URL or decoding encoded strings back to their original form. 1. btoa() MethodThis method encodes a string in base-64 and uses the "A-Z", "a-
    6 min read
  • Difference Between encodeURI and encodeURIComponent in JavaScript
    The encodeURI and encodeURIComponent functions are used to encode a URI by transforming characters that could otherwise cause issues when included in a URI. While both functions are used for encoding, they serve different purposes and encode different sets of characters. Understanding the difference
    2 min read
  • What are the builtin strings in JavaScript ?
    A sequence of letters, special characters, numbers, etc., or a combination of all of them is known as a string. Strings are created by enclosing the string characters within a single quote (') or within double quotes ("). Syntax: var myString = 'Good Morning123!'; // Single quoted string var myStrin
    3 min read
  • Convert an Array to JSON in JavaScript
    Given a JavaScript Array and the task is to convert an array to JSON Object. Below are the approaches to convert an array to JSON using JsvaScript: Table of Content JSON.stringify() methodObject.assign() methodJSON.stringify() methodThe use of JSON is to exchange data to/from a web server. While sen
    2 min read
  • Which functions are used to encode and decode JSON file in PHP ?
    JSON stands for JavaScript Object Notation. Like XML, it is a text-based format for the exchange of data which is easier to read and write and it is lighter than other formats. JSON is based on two basic structures namely Objects and Arrays. Parsing JSON data in PHP: There are built-in functions in
    2 min read
  • Convert a JavaScript Enum to a String
    Enums in JavaScript are used to represent a fixed set of named values. In JavaScript, Enumerations or Enums are used to represent a fixed set of named values. However, Enums are not native to JavaScript, so they are usually implemented using objects or frozen arrays. In this article, we are going to
    2 min read
  • Difference between decodeURIComponent() and decodeURI() functions in JavaScript
    Both decodeURI() and decodeURIComponent() are Javascript global functions that are used for decoding encoded URI (Uniform Resource Identifier). JavaScript decodeURI() function: It decodes a string previously encoded by the encodeURI() function. It returns a decoded URI by replacing each UTF-8 escape
    2 min read
  • When we use Escape Instead of encodeURI / encodeURIComponent in JavaScript ?
    A URL consists of many characters, both special and unique. Unique characters include those that are not plain standard such as spaces etc. So it means that we need to encode characters in UTF-8. So if we have a string such as: "https://www.gfg.com/what is html?" then UTF-8 will encode it as "https:
    3 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