JavaScript Basics

Source Material

See the first two chapters of the second edition of Eloquent JavaScript for more detailed information:

JavaScript

What’s JavaScript?

Rationale for Using JavaScript

Why are we learning JavaScript? → s <div class="incremental" markdown="block"> * we need something to put into version control * some understanding of basic JavaScript will be helpful in other classes, specifically with classes geared towards web development * JavaScript is a widely used, high-level programming language that is available on nearly every platform * some people (me) think it’s fun </div>

Running JavaScript Programs

What application / tool do we use to run our programs?

JavaScript Programs - File Format

What kind of file will we save our programs in (as in, what extension should we use, what’s the file type)?

A Template for Our Programs

What do we have to put in our .html file before we can write our JavaScript

We’ll have to create a bare bones web page:

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

Your code goes within the script tags!

Creating JavaScript Programs

What tool do we use to write our JavaScript programs?

Running a Program in Chrome

How do we actually run our JavaScript programs in Chrome? Where does the output (and errors) from your program show up?

Trying One Line at a Time

How do we try out single lines of JavaScript without writing an entire html file?

The JavaScript Console

Chrome’s JavaScript Console is multi-purpose. What are the ways that we can use it?

JavaScript Basics…

Values, Types and Operators

The most basic elements of a JavaScript program are:

What Exactly is a Program?

What’s a program? What’s a statement? What’s an expression?

Expressions and Statements Continued

// Statement
1 + 5;

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

All statements end in a semicolon (;) in JavaScript

Values

What’s a value, and what are some examples of values in JavaScript?

Creating Values

How do we create a value in our program?

Data Types

A type is just a kind or category of value. Here are three data types in JavaScript:

A values types sometimes determines what we can and can’t do with that value!

Operators

What’s an operator?

Numbers

Numeric Operators

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

Numeric Operators and Precedence

What is the resulting value of this expression?

6 + 20 % 8

Some Special Numbers…

Strings

What’s a string, and give an example of a string literal

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!

Empty Strings

What’s an empty string?

""

Escape Characters

How do we write a backslash, quote, tab or a new line in a string?

"\\"
"\""
"\t" 
"\n"

String Operators

We learned about one operator that works on strings. What was it?

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

"hello " + "there"

Unary Operators

Unary Operators only have a single operand to the right:

Let’s try some of these.

+10
typeof "hi there"

typeof

Again, typeof is:

What are the types that we know? What would we get with this code:

typeof "five"
typeof 5
typeof "5"

Functions

What’s a function? What’s a function call? What are arguments?

Calling Functions

How do you call or execute a function?

prompt("hello") is a function call

Built-in Functions

JavaScript comes with several built in functions. We learned two of them. What are they and what do they do?

Variables

What are variables and how do you use them in JavaScript?

var x = 23;

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

Using Prompt With Variables

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!?");

Comments

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

// a comment
/*
a long
comment
*/

A Sample Program

What do you think this program does?

<!DOCTYPE html>
<html>
<body>
<script>
var answer = prompt("Give me something to say twice:");
console.log(answer + " " + answer);
</script>
</body>
</html>

Let’s write it together.