JavaScript
JavaScript is a dynamic, object-oriented, prototype-based, weakly typed language commonly used for scripting in web browsers.
It runs on nearly every OS, and a JavaScript engine is included in almost every mainstream web browser. It was originally called LiveScript, and was developed by Netscape, but they named it JavaScript due to Netscape’s friendship with Sun Microsystems.
Standalone JavaScript engines or interpreters are available as well, including:
- Mozilla offers Rhino, an implementation of JavaScript built in Java.
- Google’s JavaScript interpreter, V8, is used in Google Chrome, node.js (a platform which enables server-side applications to be written in JavaScript) and other projects.
- Windows includes a JavaScript variant in its Windows Script Host jscript.
- WebKit (excluding Chromium) implements the JavaScriptCore engine javascriptcore.
The Mozilla Developer Network offers good documentation on JavaScript.
JavaScript is typically used to manipulate the Document Object Model (DOM) and Cascading Style Sheets (CSS) within the browser, offering user interface scripting, animation, automation, client-side validation, and much more.
Nomenclature
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 ECMA-262 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 do not distinguish the JavaScript and JScript variations from ECMAScript.
When asking a JavaScript question, you should:
- Isolate the problematic code and reproduce it in an online environment such as jsFiddle or JS Bin.
- If a library or framework is used, then tag the question with the appropriate tags: jquery for jQuery, prototypejs for Prototype, mootools for MooTools, and so on. However, if a framework is not used/necessary, do not include these tags.
- Mention which browser the code is having problems on, and what error messages, if any, were thrown by the browser. If the question is browser-specific, use tags firefox, google-chrome, internet-explorer, etc.
- Only tag the question as css or html 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.
Learning JavaScript
- Eloquent JavaScript: A Modern Introduction to Programming
- JavaScript Programming Patterns: Collection of Design Patterns for JavaScript
- The MDN JavaScript Guide: A Comprehensive JavaScript Guide at the Mozilla Developer Network
- JavaScript tutorial: A Pure JavaScript book that covers nearly everything.
- JavaScript Core Skills from the Web Standards Curriculum
- quirksmode.org | Introduction to Events: A comprehensive description of the various types of event handling. Includes overview of different ways to attach event handlers and points out quirks between different browsers. A must-read if you want to understand event handling.
- Codecademy | JavaScript: Free interactive JavaScript courses.
Wisdom from the Stack
- What should every JavaScript programmer know?
- Hidden Features of JavaScript?
- How do JavaScript closures work?
- Return value from function with an Ajax call
- JavaScript: var functionName = function() {} vs function functionName() {}
- How does the (function() {})() construct work and why do people use it?
Useful links
- W3C DOM Core, HTML, events and CSS compatibility tables from http://www.quirksmode.org
- JSLint Code Quality Tool by Douglas Crockford (and JSHint, a community-driven branch of the original)
- Code minifiers/obfuscators: /packer/, YUI Compressor, Google Closure Compiler, UglifyJS
- Code formatter/deobfuscator: JSBeautifier
- Idioms and Gotchas: Rounding, Date Object, Number Object, Scope Chain, Namespacing
- JavaScript Garden
- comp.lang.javascript FAQ: Very extensive guide on JavaScript quirks created by Usenet’s comp.lang.javascript
- ECMA 262-5 Online: HTML version of ECMA’s 262-5 Spec.
- Annotated ES5: Annotated and hyperlinked HTML derivative of the ECMAScript 5 specification.
- ECMAScript Support Matrix: In-depth feature list for ECMAScript implementations.
- Script Tokenization: Comprehensive study of syntax commonality
Free JavaScript Programming Books
- Crockford’s JavaScript
- Eloquent JavaScript
- Essential JavaScript & jQuery Design Patterns for Beginners
- JavaScript Essentials
- jQuery Fundamentals (starts with JavaScript basics)
- Mozilla Developer Network’s JavaScript Guide
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: Every structure is different but how to access them is the same. First you have to parse the JSON into a JavaScript object or array. Then access the correct properties or array indexes. To learn about object and arrays, have a look at the MDN JavaScript guide.
It is advisable to inspect the structure step by step to figure out which property or index you have to access, i.e. console.dir(x)
, console.dir(x.y)
, console.dir(x.y[2])
etc.
Q: How can I pass a PHP array to JavaScript?
A: Pass a PHP array to a JavaScript function
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: setTimeout()
inside for
always using last value?
A: setTimeout in a for-loop and pass i as value
NOTE: The content of this tag was originally posted at StackOverflow.com
- Betty asked 15 years ago
- last active 15 years ago