JavaScript/Lexical Structure

From Wikibooks, the open-content textbooks collection

< JavaScript
Jump to: navigation, search

Contents

[edit] Case Sensitivity

JavaScript is case sensitive. This means that Hello(); is not the same as HELLO(); or hello();

[edit] Whitespace

Whitespace is extra indents, line breaks, and spaces. Javascript ignores it, and it makes the code easier for humans to read. However, it can add up to a lot of space so you shouldn't use too much of it.

[edit] Comments

You can write notes in your code to help other people understand it. You can also "comment out" code that you want to hide from the parser but you don't want to delete.

One line comments

"//" hides all following text on the same line from the JavaScript interpreter.

// Pops-up "Hello, World!".
alert("Hello, World!")

Multi-line comments

"/*" starts a multi-line comment, "*/" ends it.

/*
This is a multi-line comment.
Blah Blah Blah.<br> */

[edit] Semicolons

In many computer languages semicolons are required at the end of each code statement. In Javascript you can start a new line to indicate the end of the statement and/or you can use a semicolon. You would to have a semicolon at the end of each statement in a bookmarklet because you cannot have more than one line.

[edit] Literals

A literal is a piece of data that appears directly in a program, as opposed to being stored in a variable.

A literal:

alert("Hello, World!");

NOT a literal:

var greeting = "Hello, World!";
alert(greeting);

[edit] Identifiers

An identifier is a name for a piece of data such as a variable, array or function.

[edit] Identifier Rules

  • Letters, dollar signs, underscores and numbers are allowed in identifiers.
  • The first character can NOT be a number.


Examples of legal identifiers:

  • u
  • $hello
  • _Hello
  • hello90

[edit] Reserved Words

See Reserved Words for a list of reserved words.

Personal tools
Create a book
  • Add wiki page
  • Collections help
In other languages