Hello World
To get started with writing JavaScript, open the Scratchpad and write your first "Hello world" JavaScript code:
function greetMe(yourName) {
alert("Hello " + yourName);
};
Select the code in the pad and hit Ctrl+R to watch it unfold in your browser!
Variables
Variables are used as symbolic names for values in your application. The names of variables, called identifiers, conform to certain rules.
A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9). Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase).
Variables names are commonly assigned using what is called Camel case. Camel case is the practice of writing phrases such that each word or abbreviation in the middle of the phrase begins with a capital letter, with no intervening spaces or punctuation. Common examples include "iPhone" and "eBay". The greetMe function in the Hello World section is an example of Camel case.
You can use ISO 8859-1 or Unicode letters such as å and ü in identifiers. You can also use the Unicode escape sequences as characters in identifiers. Some examples of legal names are Number_hits, temp99, and _name.
Declaring Variables
A variable may be declared in three ways:
var
let
const
An example of variable declaration using the "var" keyword:
var x = 42;
This syntax can be used to assign both local and global variables.
By simply assigning a variable, such as:
x = 13;
This always declares a global variable. It generates a strict JavaScript warning. You shouldn't use this variant.
Here's an example using the "let" keyword.
let warmGreetings = "Hello World";
This syntax can be used to declare a block scope local variable. See Variable scope below.
Variable Scope
When you declare a variable outside of any function, it is called a global variable, because it is available to any other code in the current document. When you declare a variable within a function, it is called a local variable, because it is available only within that function.
JavaScript before ECMAScript 2015 does not have block statement scope; rather, a variable declared within a block is local to the function (or global scope) that the block resides within. For example the following code will log 5, because the scope of x is the function (or global context) within which x is declared, not the block, which in this case is an if statement.
if (true) {
var x = 5;
};
console.log(x); // 5
This behavior changes, when using the let declaration introduced in ECMAScript 2015.
if (true) {
let y = 5;
};
console.log(y); // ReferenceError: y is not defined
Global Variables
Global variables are in fact properties of the global object. In web pages the global object is window, so you can set and access global variables using the window.variable syntax.
Consequently, you can access global variables declared in one window or frame from another window or frame by specifying the window or frame name. For example, if a variable called phoneNumber is declared in a document, you can refer to this variable from an iframe as parent.phoneNumber.
Constants
You can create a read-only, named constant with the const keyword. The syntax of a constant identifier is the same as for a variable identifier: it must start with a letter, underscore or dollar sign and can contain alphabetic, numeric, or underscore characters.
const PI = 3.14;
- A constant cannot change value through assignment or be re-declared while the script is running. It has to be initialized to a value.
- The scope rules for constants are the same as those for let block scope variables. If the const keyword is omitted, the identifier is assumed to represent a variable.
You cannot declare a constant with the same name as a function or variable in the same scope. For example:
// THIS WILL CAUSE AN ERROR
function f() {};
const f = 5;
// THIS WILL ALSO CAUSE AN ERROR
f() {
const g = 5;
var g;
};
However, object attributes are not protected, so the following statement is executed without problems
const MY_OBJECT = {"key": "value"};
MY_OBJECT.key = "otherValue";