Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript
communitycreator

What is encodeURI() in JavaScript?

Fatima Hasan

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The encodeURI() function encodes a URI by replacing characters in the given string with escape sequences. The characters that are left as they are and are not encoded are given below:

  • All alphabets (including uppercase and lowercase)
  • All digits from 0-9
  • Other characters ; , / ? : @ & = + $ - _ . ! ~ * ’ ( ) #

An example of an escape sequence is %20, which is used to replace the space character, i.e., " ".

Parameters

encodeURI() takes a string to be encoded as input.

Syntax

encodeURI(str)

Return value

encodeURI() returns the encoded URI.

Example

console.log ( encodeURI("https://www.google.com/search?q=encodeuri example åел") )

Usage

Another similar function in JavaScript is encodeURIComponent(). Choosing which one to use can be a source of confusion if one does not understand the difference.

  • encodeURI is used when the string to be encoded consists of a complete URL. Therefore, “/” is not included because different parts of a URL are separated by “/”, such as the hostname, path, and query strings.
  • encodeURIComponent is used to encode only a part of the URL, for example, the query string. Hence, the list of characters replaced by escape sequences is greater than that in encodeURI.

RELATED TAGS

javascript
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring