JavaScript Basics

Some Readings

Most of the material in these slides is based on the frist two chapters of the second edition of Eloquent JavaScript:

A Quick Note on Writing JavaScript

<!DOCTYPE html>
<html>
<body>
<script>
// your JavaScript goes here!
</script>
</body>
</html>

Running Your Programs

We’ll run our programs in our web browser!

Let’s see how that works.

Trying One Line at a Time

The JavaScript console also lets you try out lines of JavaScript one line at a time interactively.

Let’s check out the interactive part of the JavaScript console.

The JavaScript Console

As you can see, Chrome’s JavaScript console is multi-purpose. What are the ways that we can use it?

Ok. On to JavaScript…

Values, Types and Operators

The most basic elements of a JavaScript program are:

Values

Values are just data. Some examples of data we’ll see in JavaScript are:

Values in Programs

Some examples of values in programs include...

Numbers

Operators

Numeric Operators

Some binary arithmetic operators (they take two operands, one on either side)

Let’s try some of these out.

Numeric Operators, Precedence

What order would I evaluate this in? What is the resulting value?

4 + 1 * 5

Strings

A string is an ordered sequence of characters. You can tell that a value is a string if it is surrounded by single or double quotes:

"I am a string"
"I'm a string"

Quoted text is a string!

Strings Continued

A string can be composed of any characters: numbers, letters, punctuation, spaces, etc.

The following is a string with nothing in it… or an empty string:

""

Escape Characters

If there is a backslash in a string (\), that means:

For example, \n is a newline

"\n"

For example, \t is a tab

"\t" 

I Heard You Like Backslashes

How would we put a double quote in a double quoted string?

"\""

And what about an actual backslash?

"\\"

String Operators

string concatenation, or +, is an operator that takes two strings and joins them:

"hello " + "there"

Expressions and Statements

A program is a sequence of statements that specify to a computer actions to perform.

A statement corresponds to a sentence… it’s a full instruction for the computer.

All statements end in a semicolon in JavaScript.

An expression is just a fragment of code that produces a value; it’s not a statement by itself.

Expressions and Statements Continued

// Expression (no semicolon)
1 + 5

// Statement
1 + 5;

// Even these are statements:
1;
"hi";

All statements end in a semicolon in JavaScript

Functions

What’s a function?

To call a function, use its name followed by parentheses.

Some Built-in Functions

Let’s try these out.

Variables

Variables are names that represent particular values:

var x = 23;

// using that variable
console.log(x + 7);

What will the above output?

Variables Cont.

Variables can be assigned to different kinds of values! For example:

Rules for Naming Variables

Prompt Revisited

Remember that the function prompt gives back a value. To retain that value, you have to hold it in a variable:

var answer = prompt("Yes or no!?");

You Can Put All of These Together!

Let’s write a small program that echoes back whatever is written in a prompt.

var answer = prompt("Say something!");
console.log(answer);

Of course, you have to embed that in script tags in HTML for it to run (or just use the interactive console).

Comments

Comments allow the programmer to write notes that the JavaScript interpreter will skip over / ignore

// a comment
/*
a long
comment
*/

Some things to remember!