JavaScript syntax: Difference between revisions

Content deleted Content added
m Disambig "hack"
Quamaretto (talk | contribs)
Improved quality of beginning of article, still needs a true introduction and it's lacking completion and accuracy (e.g. Regular Expression literals). DOM hacks aren't really syntax.
Line 1:
The '''syntax of JavaScript''' is a set of rules that defines how a [[JavaScript]] program will be written and [[Interpreter (computing)|interpreted]].
 
==Data structures==
===Variables===
[[Variable]]s in standard JavaScript have no type attached, and any value can be stored in any variable. Variables can be declared with a <code>var</code> statement. These variables are [[Variable#Scope_and_extent|lexically scoped]] and once a variable is declared, it may be accessed anywhere inside the function where it is declared. Variables declared outside any function, and variables used without being declared with 'var', are global (can be used by the entire program).
[[Variable]]s are generally dynamically typed.
Variables are defined by either just assigning them a value or by using the <code>var</code> statement.
Variables declared outside of any function, and variables declared without the <code>var</code> statement, are in "global" scope, visible in the entire web page; variables declared inside a function with the <code>var</code> statement are local to that function.
 
Here is an example of variable declarations and global values:
To pass variables from one page to another, a developer can set a [[HTTP cookie|cookie]] in the user's browser. More complicated web applications will often store data in a hidden frame or [[iframe]].
 
<code>
A third way to pass variables between pages is to include them in the call for the next page. The list of arguments, called the query string, is preceded by a question mark, and each argument specification follows the format: ''name=value''. The ampersand character is used as the list separator character. An example of this technique is ''<code>sample.html?arg1=foo&arg2=bar</code>''. The following page can then extract these values by performing [[regular expression]] operations on the <code>window.___location</code> property.
x = 0; // A global variable;
var y = 'Hello!'; // Another global variable
 
function f(){
There is also an old [[Hack (technology slang)|hack]] that still works on most browsers: the '''window.name''' property can be set to string data which can then be retrieved by the next page to load into the window.
var z = 'foxes'; // A local variable;
twenty = 20; // Another global
return x; // We can use x here because it is global
}
// The value of z is no longer available
</code>
 
==Basic data types==
 
===Numbers===
Line 47 ⟶ 54:
[[Array]]s are implemented so that only the elements defined use memory; they are "[[sparse array]]s". Setting <code>myArray[10] = 'someThing'</code> and <code>myArray[57] = 'somethingOther'</code> only uses space for these two elements, just like any other object. The <code>length</code> of the array will still be reported as 58.
 
===Strings===
Object literals allow one to define generic structured data:
Strings in Javascript are a sequence of characters. Strings in JavaScript can be created directly by placing the series of characters between double or single quotes.
 
var greeting = "Hello, ronald!";
var another_greeting = 'Greetings, people of Earth.';
 
Individual characters within a string can be accessed (as strings with only a single character) through the same notation as arrays:
 
var h = greeting[0]; // Now h contains 'H'
 
===Objects===
 
The most basic objects in JavaScript act as dictionaries. These dictionaries can have any type of value paired with a key, which is a string. Objects with values can be created directly through object literal notation:
 
var o = {name: 'My Object', purpose: 'This object is utterly without purpose.', answer: 42};
 
Properties of objects can be created, set, and read individually using the familiar dot ('.') notation or by a similar syntax to arrays:
 
var name = o.name; // name now contains 'My Object'
var answer = o['answer'] // answer now contains 42
 
Object literals and array literals allow one to defineeasily genericcreate structuredflexible data structures:
 
var myStructure = {
Line 58 ⟶ 86:
};
 
This is the basis for [[JSON]], which is a simple notation that uses JavaScript-like syntax for data exchange.
This syntax has its own standard, [[JSON]].
 
==Operators==