Nov
12
2008
Javascript, Rules For The Road Ahead
Author: mprokesCommenting Your Code
Comments will always be an important part of programming; they are often used to describe whats going on in your code, and even for automatically generating documentation.
There are two styles of comments:
- //Single Line
- /*
Multi
Line
*/
Some issues with multi-line comments:
Since multi-line comments end with */ this means that you may never include a */ within the body of the comment.
/*An error is caused when I use a */ as you can see.*/
Reserved Words
Reserved Words are used by the javascript programming language to identify programming structures, and execute instructions based on those structures.
There are 59 reserved words identified by the language as follows:
abstract, boolean, break, byte, case, catch, char, class, const, continue, debugger, default, delete, do, double, else, enum, export, extends, false, final, finally, float, for, function, goto, if, implements, import, in, instanceof, int, interface, long, native, new, null, package, private, protected, public, return, short, static, super, switch, synchronized, this, throw, throws, transient, true, try, typeof, var, volatile, void, while, with
I am also including (undefined, NaN, and Infinity) as being reserved, though they serve a different purpose of describing data rather than structure.
Some Notes On Reserved Words:
- Not all reserved words are used by the langauge, so you will only need to memorize the functionality of a small subset.
- Do not use reserved words to name object literals, properties, variables, parameters, or any other programming structure for that matter.
Numbers
Numerical values are and will continue to be used throughout your development career. Thus I suggest that you become familiar with the properties of javascript numerical values.
Numbers Are:
- Wrapped up as objects when inspection is required, but are treated as primitives!
- 64bit
- Represented by a single object type (meaning an integer value of 1 is also 1.0 and a boolean value of true).
- Covers classic number types such as int, float, boolean, double, and long
Integers are represented by a numerical value, but are not followed by a decimal place.
- 123
Decimals are represented by numerical values containing a decimal place.
- 123.456
Exponents are represented by a numerical value followed by an e and a positive or negative decimal placing.
- 123e-3 (represents .123)
Negative numbers are represented with a – followed by a numerical value.
- -123
Infinity is represented by numbers greater than 1.79769313486231570e+308
Strings
Strings represent data specifying a set of characters. Strings are also the most commonly used data for most languages. Javascript is no exception, whether creating javascript object literals or validating form data you will find that strings pose an important role in your programming career. Strings are objects and contain important functions for manipulation and objects details.
Strings can be specified through two methods:
- “This is a string!”
- ‘This is a string!’
Why two ways of writing strings?
This is because at the time javascript was created, it was not an uncommon perception that it may be integrated tightly with html. So if you have a tag that uses an onclick=”alert(“oops”)” this would escape the parameter before needed. This may be rectified by escaping the quotes using \” but it is often more easy to just use a single ‘ as seen here onclick=”alert(‘yeey’)”. The duality of quoting often cancles its self out very nicely, so as to not impede programming.
Some other notes about strings:
- Strings are immutable
- Concatination is done with a + operator (ex: “hello “+”my “+”name “+”is “+”matt “)
- Strings are objects
- You may escape something within a string using a \ (ex “I want to see a \\ or a \” “)