Questions tagged [javascript]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs] etc.

JavaScript (a dialect of ECMAScript) is a high-level, dynamic, multi-paradigm, object-oriented, prototype-based, weakly-typed, and interpreted language traditionally used for client-side scripting in web browsers. JavaScript can also be run outside of the browser with the use of a framework like Node.js, Nashorn, Wakanda, or Google Apps Script. Despite the name, it is unrelated to the Java programming language and shares only superficial similarities.

When asking a JavaScript question, you should:

  1. Debug your JavaScript code (see Creativebloq, MDN, Google, & MSDN).
  2. Isolate the problematic code and reproduce it in a Stack Overflow code snippet or an external online environment such as JSFiddle, JS Bin or PasteBin (remember to also include the code in the question itself).
  3. If a library or framework is used, then tag the question with the appropriate tags: for jQuery, for Prototype, for MooTools, and so on. However, if a framework is not used or necessary, do not include these tags.
  4. If the issue is client-side, mention which browser the code is having problems on, and what error messages, if any, were thrown by the browser. Use the Developer Tools for your browser (see "Useful Tools" below) to see these messages. If the question is browser-specific, use tags , , , , , , etc.
  5. Only tag the question as or if you are asking about an issue that concerns the combination of one of those with JavaScript and could only be answered with information specifically regarding either of those subjects.

Note: Unless a tag for a framework or library is also included, a pure JavaScript answer is expected for questions with the tag.

About JavaScript

JavaScript runs on nearly every operating system, and an engine is included in mainstream web browsers. Developed in 1995 by Brendan Eich at Netscape Communications, it was originally called LiveScript but was renamed to JavaScript due to Netscape's friendly relationship with Sun Microsystems (creators of Java) at the time.

Standalone JavaScript engines or interpreters are available as well, including:

  • Mozilla's Spidermonkey, the first JavaScript engine ever written, currently used in Mozilla Firefox.
  • Google's JavaScript engine, Chrome V8, used in Google Chrome and Chromium.
  • Google Apps Script, a cloud-based/server-side interpreter that provides programmatic "macro-like" control of Google Apps services and documents.
  • Node.js, built on top of V8, a platform that enables server-side applications to be written in JavaScript.
  • Windows includes JScript, a JavaScript variant in Windows Script Host.
  • Chakra, a fork of JScript, is developed by Microsoft and used in Edge.
  • Mozilla also offers Rhino, implementation of JavaScript built-in Java, typically embedded into Java applications to provide scripting to end-users.
  • WebKit (except for the Chromium project) implements the JavaScriptCore engine.
  • ActionScript (originally derived from HyperTalk) is now an ECMAScript dialect and uses a lot of ECMAScript APIs.
  • Duktape Embeddable, a portable ECMAScript engine in C with a small memory footprint.
  • Wakanda, a framework, IDE, and Server built on V8 supporting server-side JavaScript
  • Meteor: a Meteor application is a mix of client-side JavaScript that runs inside a web browser or PhoneGap mobile app, server-side JavaScript that runs on the Meteor server inside a Node.js container. (according to MeteorJS documentation)
  • Nashorn, a JavaScript engine developed in the Java programming language, based on the Da Vinci Machine (JSR 292)

The Mozilla Developer Network contains high-quality documentation on JavaScript.

JavaScript is typically used to manipulate the Document Object Model (DOM) and Cascading Style Sheets (CSS) within the browser. This allows user interface scripting, animation, automation, client-side validation, and much more.

With the recent emergence of platforms such as Node.js, JavaScript can now be used to write server-side applications. In addition, it is also used in environments that aren't web-based, like PDF documents, site-specific browsers, desktop widgets, etc.

Nomenclature

Although it was developed under the name Mocha, the language was officially called LiveScript when it first shipped in beta releases of Netscape Navigator 2.0 in September 1995, but it was renamed JavaScript when it was deployed in the Netscape browser version 2.0B3.

The change of name from LiveScript to JavaScript roughly coincided with Netscape adding support for Java technology in its Netscape Navigator web browser. The final choice of name caused confusion, giving the impression that the language was a spin-off of the Java programming language, and the choice has been characterized as a marketing ploy by Netscape to give JavaScript the cachet of what was then the hot, new web-programming language.

People often use the term JavaScript informally. The language and the term originated from Netscape. ECMAScript, JavaScript, and JScript are terms that are easy to confuse.

ECMAScript was developed as a standardization of Netscape's JavaScript and Microsoft's independently-developed JScript. The canonical reference is the ECMAScript® 2015 Language Specification. While JavaScript and JScript aim to be compatible with ECMAScript, they also provide additional features (and other deviations) not described in the ECMA specifications. Other implementations of ECMAScript also exist.

The differences today for those who use JavaScript are negligible; people generally do not distinguish the JavaScript and JScript variations from ECMAScript.

ECMAScript versions

Most modern browsers implement JavaScript based on the ECMAScript 6 specification, although some fail to implement some ES6 features. However, older browsers such as Internet Explorer 8 implement the ECMAScript 3 specification, which does not contain functions such as Function.prototype.bind, and even JSON.parse, among others. You can see the current browser support of ES6 features

The current version of ECMAScript is ECMAScript 10, properly known as ECMAScript 2019, which was finalized in June 2019.

Example JavaScript code

This script displays "Hello World" on your screen.

window.onload = function() {
   alert('Hello World!');
};

Demo!

Learning JavaScript

Interactive JavaScript learning

Free JavaScript Programming Books

Videos

Security

JavaScript and the DOM provide the potential for malicious authors to deliver scripts to run on a client computer via the Web. Browser authors contain this risk using two restrictions. First, scripts run in a sandbox in which they can only perform web-related actions, not general-purpose programming tasks like creating files. Second, scripts are constrained by the same-origin policy: scripts from one Web site do not have access to information such as usernames, passwords, or cookies sent to another site. Most JavaScript-related security bugs are breaches of either the same origin policy or the sandbox.

Content Security Policy is the main intended method of ensuring that only trusted code is executed on a Web page.

XSS is a frequent type of attempting to steal data or harm a website via JavaScript.

Useful Tools

Wisdom from Stack Overflow

Useful links

Frequently Asked Questions

Find some answers to some of the more frequently asked questions about JavaScript and related technology below.

Q: I have this JSON structure, how can I access property x.y.z?
A: How can I access and process nested objects, arrays or JSON?

Q: I'm adding events in a for loop but all handlers do the same thing, why?
A: JavaScript closure inside loops – simple practical example

Q: I want to compare something against multiple values, is there an easy way to do it?
A: Concise way to compare against multiple values

Q: How to set up proper inheritance?
A: Objects don't inherit prototyped functions

Q: How do JavaScript closures work?
A: How do JavaScript closures work?

Q: Why does setTimeout() inside a for loop always use the latest value?
A: setTimeout in for-loop does not print consecutive values

Q: How to return the response from an AJAX call from a function?
A: How do I return the response from an asynchronous call?

Q: Why don't my handlers hooked up in a loop work correctly, and what can I do about it?
A: Javascript: generate dynamically handler

Q: How can I get query string values?
A: How can I get query string values in JavaScript?

Q: What does use strict do in JavaScript?
A: What does "use strict" do in JavaScript, and what is the reasoning behind it?

Q: How can I make a redirect page in jQuery/JavaScript?
A: How do I redirect to another webpage?

Q: How to sort an array of objects by a property value?
A: Sort array of objects by string property value

Q: From an array of objects, how do I extract the value of a property as an array? A: From an array of objects, extract value of a property as array

Q: I'm adding elements with JavaScript or jQuery at a later point and adding events but they're not firing, why?
A: You might want event delegation.

Q: How can I only keep items of an array that match a certain condition?
A: How can I only keep items of an array that match a certain condition?

Q: How can I debug my JavaScript code?
A: How can I debug my JavaScript code?

Q: What does this symbol mean in JavaScript?
A: What does this symbol mean in JavaScript?

More information

Chat Room

2222634 questions
9441
votes
109 answers

How can I remove a specific item from an array?

I have an array of numbers and I'm using the .push() method to add elements to it. Is there a simple way to remove a specific element from an array? I'm looking for the equivalent of something like: array.remove(number); I have to use core…
Walker
  • 104,797
  • 22
  • 64
  • 92
8089
votes
60 answers

How do I check if an element is hidden in jQuery?

Is it possible to toggle the visibility of an element, using the functions .hide(), .show() or .toggle()? How would you test if an element is visible or hidden?
Philip Morton
  • 121,023
  • 36
  • 84
  • 96
7854
votes
28 answers

What does "use strict" do in JavaScript, and what is the reasoning behind it?

Recently, I ran some of my JavaScript code through Crockford's JSLint, and it gave the following error: Problem at line 1 character 1: Missing "use strict" statement. Doing some searching, I realized that some people add "use strict"; into their…
Mark Rogers
  • 90,043
  • 18
  • 79
  • 129
7711
votes
58 answers

How do I redirect to another webpage?

How can I redirect the user from one page to another using jQuery or pure JavaScript?
venkatachalam
  • 101,405
  • 29
  • 70
  • 76
7629
votes
86 answers

How do JavaScript closures work?

How would you explain JavaScript closures to someone with a knowledge of the concepts they consist of (for example functions, variables and the like), but does not understand closures themselves? I have seen the Scheme example given on Wikipedia,…
e-satis
  • 515,820
  • 103
  • 283
  • 322
7419
votes
3 answers

How to check whether a string contains a substring in JavaScript?

Usually I would expect a String.contains() method, but there doesn't seem to be one. What is a reasonable way to check for this?
gramm
  • 18,623
  • 6
  • 24
  • 25
7174
votes
39 answers

var functionName = function() {} vs function functionName() {}

I've recently started maintaining someone else's JavaScript code. I'm fixing bugs, adding features and also trying to tidy up the code and make it more consistent. The previous developer used two ways of declaring functions and I can't work out if…
Richard Garside
  • 82,523
  • 9
  • 75
  • 82
6594
votes
34 answers

How do I remove a property from a JavaScript object?

Say I create an object as follows: let myObject = { "ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*" }; How should I remove the property regex to end up with new myObject as follows? let myObject = { "ircEvent": "PRIVMSG", …
johnstok
  • 87,006
  • 12
  • 51
  • 76
5960
votes
42 answers

How to return the response from an asynchronous call?

I have a function foo which makes an asynchronous request. How can I return the response/result from foo? I am trying to return the value from the callback, as well as assigning the result to a local variable inside the function and returning that…
Felix Kling
  • 705,106
  • 160
  • 1,004
  • 1,072
5660
votes
49 answers

Which equals operator (== vs ===) should be used in JavaScript comparisons?

I'm using JSLint to go through JavaScript, and it's returning many suggestions to replace == (two equals signs) with === (three equals signs) when doing things like comparing idSele_UNVEHtype.value.length == 0 inside of an if statement. Is there a…
bcasp
  • 57,427
  • 4
  • 17
  • 14
5629
votes
62 answers

How do I include a JavaScript file in another JavaScript file?

Is there something in JavaScript similar to @import in CSS that allows you to include a JavaScript file inside another JavaScript file?
Alec Smart
  • 85,672
  • 35
  • 115
  • 178
5175
votes
67 answers

What is the most efficient way to deep clone an object in JavaScript?

What is the most efficient way to clone a JavaScript object? I've seen obj = eval(uneval(o)); being used, but that's non-standard and only supported by Firefox. I've done things like obj = JSON.parse(JSON.stringify(o)); but question the efficiency. …
jschrab
  • 11,035
  • 4
  • 18
  • 17
5141
votes
38 answers

What's the difference between using "let" and "var"?

ECMAScript 6 introduced the let statement. I've heard that it's described as a local variable, but I'm still not quite sure how it behaves differently to the var keyword. What are the differences?. When should let be used instead of var?
TM.
  • 94,986
  • 30
  • 119
  • 125
5023
votes
40 answers

For-each over an array in JavaScript

How can I loop through all the entries in an array using JavaScript? I thought it was something like this: forEach(instance in theArray) Where theArray is my array, but this seems to be incorrect.
Dante1986
  • 52,129
  • 12
  • 34
  • 53
4865
votes
99 answers

How to validate an email address in JavaScript

Is there a regular expression to validate an email address in JavaScript?
pix0r
  • 30,601
  • 18
  • 82
  • 102
1
2 3
99 100