Home > 2010 Summer BTI220 > Learning JavaScript – the top ten list

Learning JavaScript – the top ten list


When we begin to learn a new computer programming language, we often try to get acquainted with the language by applying our current knowledge to its characteristics. This “top ten” list of questions and answers helps with this process.

As a first-year Software Development student, you have some experience with the C programming language. Let’s begin to learn about JavaScript by asking questions in the form of a “top ten” list. (Note that you can ask these questions about ANY language.)

.

The “top ten” list of questions

Here is the “top ten” list of questions to ask when learning JavaScript, when used with web pages:

1. How do I create/edit/save the source code? What editor do I use? Where do I store the source code? What is the source code file naming convention?

2. How do I build/compile and then execute/run my program? Is there a compiler? Is there a host execution (aka “runtime”) environment?

3. What is the program’s entry point? Syntactically, what coding convention must I follow?

4. What data types can I use? Are they categorized (e.g. value/stack, reference/heap), and if so, what do I need to know about their characteristics (e.g. size, initial value, precision, convertibility, etc.)?

5. How do I declare new variables/fields of a data type? How do I instantiate them? How do I refer to and use these variables/fields? (Note that a type could be a class declaration for an object-oriented language and platform.)

6. What operators are available to me in this language? (How do they compare to other languages that I’m familiar with?)

7. What is the syntax of a statement, and of an expression? What delimiters are used? How is scope and hierarchy expressed?

8. What syntax enables me to process a decision (e.g. if-else, or switch-case)?

9. What syntax enables me to process repeatedly (e.g. for, while)?

10. How are functions (methods) declared? What rules are there for argument/parameter declaration? How are functions used/called? Are the arguments passed by value, or by reference?

.

The answers

Here are the answers. We cover them in class, and we go through some examples.

1. How do I create/edit/save the source code? What editor do I use? Where do I store the source code? What is the source code file naming convention?

Although web client programming allows us to place JavaScript inline or internal to a document (in a way that’s similar to CSS rules), we prefer that you store your JavaScript code in its own source code file.

You can use any text editor or programmer’s editor.

We suggest that you store your code in the ui/js folder, and name each source code file by using a .js extension.

.

2. How do I build/compile and then execute/run my program? Is there a compiler? Is there a host execution (aka “runtime”) environment? Does it control the application lifecycle?

Your browser has a JavaScript runtime environment. Your code runs in the security context of the browser. It has limitations on the operations it can perform.

JavaScript is an interpreted language.

Your goal with JavaScript should be to write “event handlers” (as functions), so that they can handle events that happen during the page’s usage lifecycle.

.

3. What is the program’s entry point? Syntactically, what coding convention must I follow?

If you organize your JavaScript code as a set of event handlers, a function will be called only when requested.

There is no main() function, and no conventional program structure as you would see in a C program.

.

4. What data types can I use? Are they categorized (e.g. value/stack, reference/heap), and if so, what do I need to know about their characteristics (e.g. size, initial value, precision, convertibility, etc.)?

JavaScript is a loosely-typed language. It offers us about five data types:

  • numbers
  • strings
  • booleans
  • arrays
  • objects

The usage context of a variable can affect its type. For example, the interpreter can infer between an integer and its string representation.

.

5. How do I declare new variables/fields of a data type? How do I instantiate them? How do I refer to and use these variables/fields? (Note that a type could be a class declaration for an object-oriented language and platform.)

You must use the “var” keyword to precede a variable name. Unlike the C language, you do not need a type specifier. The variable’s initial value will set its initial type.

Examples…

Declaration syntax:

var variableName;

var variableName = value;

Referring to and using syntax:

variableName = someValue;

document.write(variableName);

.

6. What operators are available to me in this language? (How do they compare to other languages that I’m familiar with?)

A rich set of operators are available, using a comfortable C-like syntax.

.

7. What is the syntax of a statement, and of an expression? What delimiters are used? How is scope and hierarchy expressed?

JavaScript is a C-family language, so its statement syntax is similar.

Expressions are also similar, and you can use operators on objects without conversions.

Statements should end with a ; semicolon, just like in C.

Curly braces { } are used as code block delimiters. Scoping follows rules similar to C (but are different, as you will learn).

.

8. What syntax enables me to process a decision (e.g. if-else, or switch-case)?

JavaScript is a C-family language, so its statement syntax is similar. if-else (including the ? : syntax) and switch-case are available.

.

9. What syntax enables me to process repeatedly (e.g. for, while)?

JavaScript is a C-family language, so its statement syntax is similar. for and while are available.

.

10. How are functions (methods) declared? What rules are there for argument/parameter declaration? How are functions used/called? Are the arguments passed by value, or by reference?

Functions are declared like this:

function functionName(argument0, argument1, etc.) { }

In JavaScript, pass-by-value is the default. The function receives a copy of the value from the caller.

.


Advertisements
Categories: 2010 Summer BTI220
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: