Skip to content

Intro to Javascript

Cassi Lam edited this page Dec 23, 2017 · 2 revisions

Overview

Teaches the basics of Javascript. Specifically, the Javascript console, data types, string concatenation, and variables.

Table of Contents

1.1 - The Javascript Console

1.2 - Math Operators

1.3 - Data Types

1.4 - String Concatenation

1.5 - Variables

New Concepts

What is Javascript?

Javascript is one of the 3 main components of web development. It controls the behavior of a website (how it functions) and is used to make a website interactive.

1.1 - The Javascript Console

The Javascript console is mostly used as a playground to test or debug Javascript code. To load the JS Console:

Open Google Chrome
For Windows/Linux: Ctrl + Shift + J
For Mac: Cmd + Opt + J

You should see something like this:

> 

The > symbol is called the prompt and it indicates that the browser is ready to receive and execute your Javascript code.

Printing to the Console

To print (or "log") text into the JS console, we use the console.log() command.

> console.log("Hello world!");
  Hello world!

Activity: Printing to the Console In the console, use console.log() to log your first name. Then use console.log() again to log your favorite color.

1.2 - Math Operators

Javascript can be used to interpret mathematical expressions.

> 5 + 5
  10
> 3 * 3
  9
Operator Name Example
+ Addition 5 + 5 = 10
- Subtraction 10 - 2 = 8
* Multiplication 4 * 3 = 12
/ Division 6 / 3 = 2
% Modulus 5 % 2 = 1

1.3 - Data Types

Computers work by manipulating data but different kinds of data need to be handled in different ways. Data types refer to the kind of information that needs to be processed. There are many data types but we will be focusing on two of them for now: strings, numbers, and null.

STRING

A string is a set of characters surrounded by either single quotes (e.g. ‘Hello world’) or double quotes (e.g. “I love to code!” or “24”).

NUMBERS

Javascript numbers can be whole numbers (integers) or decimal numbers. e.g. 10 or 3.1415

NULL

null is a special keyword that means no value or empty.

1.4 - String Concatenation

String concatenation is when you join two strings together. Strings can be concatenated using the + sign.

> "Hello" + "world"
  "Helloworld"

You can also concatenate numbers to strings! Keep in mind that as long as one of the pieces being added is a string, the pieces will be concatenated and will return as a string.

> 5 + "apples"
  "5apples"
> "My favorite number is " + 18
  "My favorite number is 18"

1.5 - Variables

A variable is used to store information in a program. You can think of a variable as a box, and we can store data inside the box. The box has a specific name that the developer chooses. Variable names should be specific and relevant so other developers can easily understand what information it is storing. Variable_Box

Declaring Variables

To declare, or create, variables in Javascript, you must use the var keyword as show below:

> var color = "blue"

You can find out what the value of a variable is by typing the variable name in the console.

> color
  "blue"

In mathematics, the equal sign = means equality, but in programming languages it means assignment. We’re “assigning” a value to the variable.

What happens to the old value when we assign the variable something new? The old value gets replaced, and the variable now holds the new value.