Everything in JavaScript is case-sensitive.
Characters in an identifier name (variable, function or property) can be letters, numbers, underscore or dollar sign. The first character can not be a number. Of course, keywords can not be used as identifiers.
Using camelCase for identifiers is the best case practice.
Comments in JavaScript are // for single lines and /*… */ for block comments, just like in the PHP or C languages.
Although semicolons are not required at the end of a statement, they are a best practice for many reasons, such as performance and maintainability.
Blocks of multiple statements can be contained between { and }.
if
statements don’t require blocks (curly braces) but it is a best practice to use them even for one line statements.
JavaScript reserved words.
Variables can hold any type of data. Define variables with the var
operator. Before a variable is initialized, it will contain undefined
.
Switching data types on variables is acceptable. The variable can start as a number and later be assigned a string instead.
Defining a variable with var
makes that variable local to the scope in which it was defined. Omitting the var
operator will make the variable global regardless of where it’s defined. Global variables are not recommended.
You can define multiple variables in one statement separated by a comma.
Data types: Undefined, Null, Boolean, Number, String, and Object (unordered list of name-value pairs).
To find the type of a JavaScript variable use the typeof operator. It will return “undefined”, “boolean”, “string”, “number”, “object” or “function”. Use it like this:
alert(typeof $myvar); alert(typeof ($myvar));
typeof
is an operator, not a function. parenthesis are not required but can be used around the variable.