In JavaScript, escape(), encodeURI(), and encodeURIComponent() are three functions used to encode strings for different purposes. Each function serves a distinct purpose, and it’s essential to understand their differences:
escape()
The escape() function is used to encode a string so that it can be safely included in a URL query string. It encodes special characters, except for alphanumeric characters and the following set of symbols: @*_+-./. The main drawback of escape() is that it does not encode all characters, and it’s considered deprecated in favor of encodeURIComponent().
The encodeURI() function is used to encode a complete URI (Uniform Resource Identifier) but leaves the special characters used in the query string (?, &, =, etc.) untouched. It is primarily used to encode the main part of a URL, such as the protocol, domain, and path.
The encodeURIComponent() function is used to encode a component of a URI, such as a query parameter, fragment identifier, or any part that needs to be included in the query string. Unlike encodeURI(), this function encodes all special characters to ensure they are safely passed as parameters in a URL.
Use escape() for encoding a string to be safely included in a query string, but it’s deprecated and not recommended for general use.
Use encodeURI() for encoding a complete URI (protocol, domain, path) but not the query string parameters.
Use encodeURIComponent() for encoding individual components (e.g., query parameters) of a URI to ensure all special characters are encoded properly. This is the most commonly used encoding function for URLs.
Life is already very complex, so let’s simplify your projects by removing all the jQuery code. Plain JavaScript provides the same functionalities and it does not require any additional frameworks. And it’s supported by most of the modern browsers out of the box. This is a list of replacements for your daily used jQuery methods.
$() or jQuery()
Return a collection of matched elements either found in the DOM based on passed argument(s) or created by passing an HTML string.
// jQuery implementationvarelement = $(selector);// Plain JavaScriptvarelement = document.querySelector(selector); // Single elementvarelements = document.querySelectorAll(selector); // Multiple elements
.addClass()
Adds the specified class(es) to each element in the set of matched elements.
Add or remove one or more classes from each element in the set of matched elements, depending on either the class’s presence or the value of the state argument.
The code of a JavaScript library might get very complex over time. This can be a problem for maintenance and expandability.
When writing a library, you should address two main points:
Keep the environment clean from global variables and methods to avoid conflicts between different libraries
Provide public and private methods and variables
The following “template” provides a simple code structure that fulfills those points:
// Unique name of the library.MyLibrary = function() {MyLibrary = {};/* * Internal variables and methods */varvariable = 'value';functiondoSomething() {// do something }/* * Public variables and methods */MyLibrary.myVariable = true;MyLibrary.myAction = function() {// do something }// Return the library.returnMyLibrary;}
Yes, the correct answer is: all of them. Or let’s ask another question first: for which problem?
There is not “the one and only” best programming language, there are tons. This question is simply not a good one. It’s good for endless discussion, but it’s neither complete, nor does it give you details about the problem to solve. An answer will not solve the unknown problem of the author. Every coding language has it advantages when it comes to solving a specific problem. But which one? There are advantages for JavaScript, Python, PHP, Java, Swift, Dart and others. But what if the author wants to write some firmware for a micro controller? Ok, you see the point.
But why do people ask those questions?
I see a lot of such questions posted on all social platforms. And as those questions are so incomplete, there are no correct answers. The results are endless discussions about the advantages of each coding language. And this is what most of the authors want: an open question which leads to endless attention. In detail: likes and retweets to pretend the algorithms to be an interesting account. Honestly, no one reads through thousands of responses. And if yes, you only see the advantages that each comfortable developer or user sees in his area of expertise. This might lead to some short attention, but it will never create some high quality and valuable content fo the users. And that’s a pity.
And what about: iOS or Android? Windows or macOS? Python or JavaScript? Same thing! The answers are just a time wasting mess.