Deprecated and obsolete features
This page lists features of JavaScript that are deprecated (that is, still available but planned for removal) and obsolete (that is, no longer usable).
In this article
Deprecated features
These deprecated features can still be used, but should be used with caution because they are not required to be implemented by every JavaScript engine. You should work to remove their use from your code.
Some of these deprecated features are listed in theAnnex B section of the ECMAScript specification. This section is described as normative optional — that is, web browser hosts must implement these features, while non-web hosts may not. These features are likely stable because removing them will cause backward compatibility issues and break legacy websites. (JavaScript has the design goal of "don't break the web".) Still, they are not cross-platform portable and may not be supported by all analysis tools, so you are advised to not use them, as the introduction of Annex B states:
… All of the language features and behaviors specified in this annex have one or more undesirable characteristics and in the absence of legacy usage would be removed from this specification. …
… Programmers should not use or assume the existence of these features and behaviors when writing new ECMAScript code. …
Some others, albeit in the main spec body, are also marked as normative optional and should not be depended on.
HTML comments
JavaScript source, if parsed as scripts, allows HTML-like comments, as if the script is part of a<script> tag.
The following is valid JavaScript when running in a web browser (or Node.js, which uses the V8 engine powering Chrome):
<!-- commentconsole.log("a"); <!-- another commentconsole.log("b");--> More comment// Logs "a" and "b"<!-- and--> both act like//, i.e., starting line comments.--> is only valid at the start of a line (to avoid ambiguity with a postfix decrement followed by a greater than operator), while<!-- can occur anywhere in the line.
RegExp
The following properties are deprecated. This does not affect their use inreplacement strings:
$1–$9Parenthesized substring matches, if any.
input,$_The string against which a regular expression is matched.
lastMatch,$&The last matched substring.
lastParen,$+The last parenthesized substring match, if any.
leftContext,$`The substring preceding the most recent match.
rightContext,$'The substring following the most recent match.
Warning:Avoid using these static properties, as they can causeissues when interacting with external code!
Thecompile() method is deprecated. Construct a newRegExp instance instead.
The following regex syntaxes are deprecated and only available inUnicode-unaware mode. In Unicode-aware mode, they are all syntax errors:
- Lookahead assertions can havequantifiers.
- Backreferences that do not refer to an existing capturing group becomelegacy octal escapes.
- Incharacter classes, character ranges where one boundary is a character class makes the
-become a literal character. - An escape sequence that's not recognized becomes an"identity escape".
- Escape sequences withincharacter classes of the form
\cXwhereXis a number or_are decoded in the same way as those withASCII letters:\c0is the same as\cPwhen taken modulo 32. In addition, if the form\cXis encountered anywhere whereXis not one of the recognized characters, then the backslash is treated as a literal character. - The sequence
\kwithin a regex that doesn't have anynamed capturing groups is treated as an identity escape. - The syntax characters
],{, and}may appearliterally without escaping if they cannot be interpreted as the end of a character class or quantifier delimiters.
Function
- The
callerproperty of functions and thearguments.calleeproperty are deprecated and unavailable in strict mode. - Instead of accessing
argumentsas a property of a function, you should use theargumentsobject inside function closures.
Object
- The
Object.prototype.__proto__accessors are deprecated. UseObject.getPrototypeOfandObject.setPrototypeOfinstead. This does not apply to the__proto__literal key in object literals. - The
Object.prototype.__defineGetter__,Object.prototype.__defineSetter__,Object.prototype.__lookupGetter__, andObject.prototype.__lookupSetter__methods are deprecated. UseObject.getOwnPropertyDescriptorandObject.definePropertyinstead.
String
- HTML wrapper methods like
String.prototype.fontsizeandString.prototype.big. String.prototype.substrprobably won't be removed anytime soon, but it's defined in Annex B and hence normative optional.String.prototype.trimLeftandString.prototype.trimRightshould be replaced withString.prototype.trimStartandString.prototype.trimEnd.
Date
- The
getYear()andsetYear()methods are affected by the Year-2000-Problem and have been subsumed bygetFullYearandsetFullYear. - The
toGMTString()method is deprecated. UsetoUTCString()instead.
Escape sequences
- Octal escape sequences (\ followed by one, two, or three octal digits) are deprecated in string and regular expression literals.
- The
escape()andunescape()functions are deprecated. UseencodeURI(),encodeURIComponent(),decodeURI(), ordecodeURIComponent()to encode and decode escape sequences for special characters.
Statements
Thewith statement is deprecated and unavailable in strict mode.
Initializers invar declarations offor...in loops headers are deprecated and producesyntax errors in strict mode. The initializer expression is evaluated and assigned to the variable, but the value would be immediately reassigned on the first iteration of the loop.
Normally, thecatch block of atry...catch statement cannot contain any variable declaration with the same name as the variables bound in thecatch(). An extension grammar allows thecatch block to contain avar declared variable with the same name as thecatch-bound identifier, but only if thecatch binding is a simple identifier, not adestructuring pattern. However, this variable's initialization and assignment would only act on thecatch-bound identifier, instead of the upper scope variable, and the behavior could be confusing.
var a = 2;try { throw new Error();} catch (a) { var a = 1; // This 1 is assigned to the caught `a`, not the outer `a`.}console.log(a); // 2try { throw new Error(); // Note: identifier changed to `err` to avoid conflict with // the inner declaration of `a`.} catch (err) { var a = 1; // This 1 is assigned to the upper-scope `a`.}console.log(a); // 1This is listed in Annex B of the spec and hence may not be implemented everywhere. Avoid any name conflicts between thecatch-bound identifier and variables declared in thecatch block.
Obsolete features
These obsolete features have been entirely removed from JavaScript and can no longer be used as of the indicated version of JavaScript.
RegExp
The following are now properties ofRegExp instances, no longer of theRegExp constructor:
| Property | Description |
|---|---|
global | Whether or not to test the regular expression against all possible matches in a string, or only against the first. |
ignoreCase | Whether or not to ignore case while attempting a match in a string. |
lastIndex | The index at which to start the next match. |
multiline (also viaRegExp.$*) | Whether or not to search in strings across multiple lines. |
source | The text of the pattern. |
ThevalueOf() method is no longer specialized forRegExp. It usesObject.prototype.valueOf(), which returns itself.
Function
- Functions'
arityproperty is obsolete. Uselengthinstead.
Object
| Property | Description | Alternative |
|---|---|---|
__count__ | Returns the number of enumerable properties directly on a user-defined object. | Object.keys() |
__parent__ | Points to an object's context. | No direct replacement |
__iterator__ | Used withlegacy iterators. | Symbol.iterator and the newiteration protocols |
__noSuchMethod__ | A method called when a non-existent property is called as method. | Proxy |
Object.prototype.eval() | Evaluates a string of JavaScript code in the context of the specified object. | No direct replacement |
Object.observe() | Asynchronously observing the changes to an object. | Proxy |
Object.unobserve() | Remove observers. | Proxy |
Object.getNotifier() | Create a notifier object that allows to synthetically trigger a change observable withObject.observe(). | No direct replacement |
Object.prototype.watch() | Attach a handler callback to a property that gets called when the property is assigned. | Proxy |
Object.prototype.unwatch() | Remove watch handlers on a property. | Proxy |
String
- Non-standard String generic methods like
String.slice(myStr, 0, 12),String.replace(myStr, /\./g, "!"), etc. have been introduced in Firefox 1.5 (JavaScript 1.6), deprecated in Firefox 53, and removed in Firefox 68. You can use methods onString.prototypetogether withFunction.callinstead. String.prototype.quoteis removed from Firefox 37.- Non-standard
flagsparameter inString.prototype.search,String.prototype.match, andString.prototype.replaceare obsolete.
WeakMap
WeakMap.prototype.clear()was added in Firefox 20 and removed in Firefox 46. It is not possible to traverse all keys in aWeakMap.
Date
Date.prototype.toLocaleFormat(), which used a format string in the same format expected by thestrftime()function in C, is obsolete. UsetoLocaleString()orIntl.DateTimeFormatinstead.
Array
- Non-standard Array generic methods like
Array.slice(myArr, 0, 12),Array.forEach(myArr, myFn), etc. have been introduced in Firefox 1.5 (JavaScript 1.6), deprecated in Firefox 68, and removed in Firefox 71. You can use methods onArray.prototypetogether withFunction.callinstead.
Number
Number.toInteger()is obsolete. UseMath.floor,Math.round, or other methods instead.
Proxy
Proxy.createandProxy.createFunctionare obsolete. Use theProxy()constructor instead.- The following traps are obsolete:
hasOwn(bug 980565, Firefox 33).getEnumerablePropertyKeys(bug 783829, Firefox 37)getOwnPropertyNames(bug 1007334, Firefox 33)keys(bug 1007334, Firefox 33)
ParallelArray
ParallelArrayis obsolete.
Statements
for each...inis obsolete. Usefor...ofinstead.- let blocks and let expressions are obsolete.
- Expression closures (
function () 1as a shorthand offunction () { return 1; }) are obsolete. Use regularfunctionsorarrow functions instead.
Acquiring source text
ThetoSource() methods of arrays, numbers, strings, etc. and theuneval() global function are obsolete. UsetoString(), or write your own serialization method instead.
Legacy generator and iterator
Legacy generator function statements and legacy generator function expressions are removed. The legacy generator function syntax reuses thefunction keyword, which automatically becomes a generator function when there are one or moreyield expressions in the body — this is now a syntax error. Usefunction* statements andfunction* expressions instead.
Array comprehensions and generator comprehensions are removed.
// Legacy array comprehensions[for (x of iterable) x][for (x of iterable) if (condition) x][for (x of iterable) for (y of iterable) x + y]// Legacy generator comprehensions(for (x of iterable) x)(for (x of iterable) if (condition) x)(for (x of iterable) for (y of iterable) x + y)Firefox, prior to version 26, implemented another iterator protocol that is similar to the standardIterator protocol. An object is a legacy iterator when it implements anext() method, which produces a value on each call and throws aStopIteration object at the end of iteration. This legacy iterator protocol differs from the standard iterator protocol:
- The value was returned directly as the return value of calls to
next(), instead of thevalueproperty of theIteratorResultobject. - Iteration termination was expressed by throwing a
StopIterationobject, instead of through thedoneproperty of theIteratorResultobject.
This feature, along with theStopIteration global constructor, was removed in Firefox 58+. For future-facing usages, consider usingfor...of loops and theiterator protocol.
Sharp variables
Sharp variables are obsolete. To create circular structures, use temporary variables instead.