Introduction To Programming
10-152-310

Learning Plan 2 : Variables and Data Types

Introduction to JavaScript

We're using the JavaScript programming language to learn the basics of programming/development. This is a great language to learn with. All you need to use it is a modern internet browser, like Chrome or Firefox. You can use simple and free programs to write JavaScript called "text editors."

What is JavaScript?

  • JavaScript is an interpreted programming language.
  • There is a full computer program inside your browser that reads your JavaScript code and runs your code.
  • This program is called an "interpreter" and is embedded in browsers. You can't really tell that it is separate from the browser itself.
  • You tell the interpreter what to do with a list of instructions. In the beginning of the semester these instructions will be written in HTML files that are your labs and projects.
  • For JavaScript to work correctly it has to be put in HTML files correctly. We are going to start by placing our JavaScript code in a special HTML tag:
    				<script type="text/javascript">
    				//<!--[CDATA[
    				//<!--
    				// YOUR CODE STARTS AFTER THIS LINE:
    				
    				
    				// END OF YOUR CODE
    				// -->
    				//]]-->
    				</script>
                
  • Line 1: The <script> tag is what tells the browser to treat your code as JavaScript.
  • Line 2: Start of a CDATA section. This is part of XML and means we can put text in here and modern browsers will leave it alone.
  • Line 3: This is an HTML comment to hide our JavaScript code from old browsers that don't understand JavaScript.
  • Line 4-7: You put your JavaScript code in here and it will be handed to the JavaScript interpreter by the browser.
  • Line 4: This line starts with //. That makes the line a comment that will be ignored by JavaScript.
  • Line 8: End of the HTML comment.
  • Line 9: End of the CDATA section.
  • Here's a peek at coding in JavaScript: Demo: A Peek at JavaScript Coding
  • Let's have a lab on this. Here it is: 2.1 Lab 1

About Rules

In computer programming, there are different levels of rules. Here are the three we are concerned with in this course.

  1. Syntax
    • If a computer programming language enforces a rule then we call that rule "syntax". This is the strongest level of rule. You can't break these rules or your program just won't run.
  2. Convention
    • If virtually all the users of a programming language all over the world do something the exact same way then that way is called a "convention". The JavaScript language itself doesn't care about these rules. However, if everybody is using the same convention there is good reason for it. If you want to join the ranks of professionals using JavaScript then you need to follow these conventions.
    • In this course, JavaScript won't enforce these rules but your instructor will. Part of the points for projects are on conformance to the course coding standards. Go here to see those: JavaScript Coding Standards for 10-152-310
  3. Style
    • There are some ways of structuring our code where there isn't any agreement as to the best way to do it. A large group of programmers will do it one way and another large group will do it another way. When a rule is in this category we call it a "style". Styles are not enforced by JavaScript or the instructor. What is required is that you pick a style for your code and then be consistent. Make sure that you use that style in all your code. We will go over some common styles in class.

JavaScript Syntax Rules

Remember those rules about computers? They can't think and they can't figure out what you mean. You have to be very clear and you have to use their language, not yours. Programming languages are very simplistic compared to human languages, but they are very strict. You have to follow the rules of the language that are called its syntax. Here are some of the basic syntax rules of JavaScript.

In JavaScript, the word "IF" is not the same as "if". Everything has to be the correct case. This is a challenge for lots of beginning programmers. We have to start "seeing" the case of text as part of the meaning. Almost everything you learn in programming is case sensitive.

White space are characters you can type on the keyboard but you don't normally see on the screen or on paper when you print. Here are the white space characters:

  • Space
  • Tab
  • Carriage Return (officially called the "line terminator")

In JavaScript, white space usually doesn't matter. Unfortunately, there's that word "usually" in that rule. There are some situations where it matters a lot. Therefore we're are going to have a course standard for using white space. Refer to the course JavaScript Coding Standards for 10-152-310 for more details.

Comments in our code are lines of text that are ignored by JavaScript. We use these to write notes to ourselves and other programmers who see our code. This is called Documentation.

There are two kinds of comments in JavaScript, single-line and multiline.

A single-line comment starts with two forward slashes: // Example:

                
			

A multiline comment starts with /* and ends with */. These can be over many lines. Example:

                
            

Semicolons are used in JavaScript to end a line. Most of the time they are not required by JavaScript. However, they are required in a few places so as a course standard, that is also a convention in the industry, the use of semicolons at the end of statements is required.

            	
            

JavaScript statements are lines of text that contain tokens, operators, and identifiers that is understandable to the JavaScript interpreter. They are like the sentences of a programming language. When someone writes prose they write sentences that are organized into paragraphs. When we write JavaScript we write statements that are organized into blocks and functions. Here are some more details about statements.

Statements usually end with a semicolon ";"

Simple statements are one line that just does one thing:

            	
            

There are a bunch of words that have special meaning to JavaScript. They are called Reserved Words. Because of this we can't use those words in our code. Here they are:

Note: This is not an exhaustive list.
break delete function return typeof
case do if switch var
catch else in this void
continue finally instanceof throw while
default for new try with

Let's do this lab: 2.1 Lab 2

Here is the 2.1 Assignment 1