Skip to content

Seattle CoderDojo

A Free Tech Meetup for Kids

Menu
  • 2023 Reboot
  • Teaching Kids To Code
  • Volunteers
  • Frequently Asked Questions
Menu

Lesson 1 of the Javascript Reboot: Variables

Posted on April 9, 2015April 11, 2015 by Jonathan Ng

Hi everyone!

I introduced the concept of variables in Scratch last week.  Variables are extremely important in programming because they refer to values that can be changed.  Some examples you gave of variables that are in video games:

  • Character speed
  • Enemy spawn rate
  • Gravity
  • Powerup locations

A variable is a place to store a value (something fixed).  Something like 1 or “booyah” are examples of values.  The reason you’d use a variable rather than the value directly is because you may want to use it in more than one place – for example, if you input your name to be used in a greeting (and you want many people to use the same code).

Here’s how to set a variable in Scratch, and the exact same thing, done in Javascript:

scratchvsjs1

The keyword “var” allows you to declare a variable.  Also, Javascript statements require a semicolon at the end of each one.

Variables can take in fixed values, or they can take in other variables, or they can take in more complicated expressions.  In Javascript, variables are “loosely-typed” – which is a way of saying that they don’t have to be made to take a specific type.

But what’s a type?  That’s a way of classifying the data – Javascript has Numbers, Strings and Booleans as their basic types.  But while numbers and strings are simple enough, what are booleans?  A full explanation will have to wait, but at a basic level, booleans can only take 2 values – true and false.  They’ll be very useful later on.

There are also more complicated types, but we will discuss those in a future lesson.

You can use operators on numbers to produce new numbers, like +, -, *, / and %.  The % operator is the remainder operator.  You can also use the + operator on strings as well.  But what happens when you try to add a string to a number?  Try it out.

Lastly, you can compare two numbers (and it will produce a value of true or false depending whether it’s correct) by using the == (equals), != (not equals), > (greater than), < (less than), >= (greater than or equal to), and <= (less than or equal to).  However, using two equal signs will make a string and number equal as long as they are the same (eg “1” == 1 is true).  If you don’t want to do this and want them to be the same type, then you have to use === and !== operators.

Here are some more resources you can do for practice:

  • Khan Academy’s variable review here: https://www.khanacademy.org/computing/computer-programming/programming/variables/a/review-variables
  • Code Academy’s Introduction to Javascript lesson: http://www.codecademy.com/courses/getting-started-v2/0/1
  • Crunchzilla’s Code Monster lesson variables (section 3): http://www.crunchzilla.com/code-monster

and here’s this week’s assignment: http://jsfiddle.net/zeroth_hour/yj049v9g/

In order to do this week’s assignment, you’ll need a few functions:

  1. The alert function, which creates a pop up that produces text
  2. The prompt function, which returns the string that you input in the prompt
  3. The parseInt function, which converts a string to a number

Upcoming Events

  • CANCELLED: SEATTLE CODERDOJO – March 7, 2020

  • CANCELLED: SEATTLE CODERDOJO – March 14, 2020

  • CANCELLED: SEATTLE CODERDOJO – March 21, 2020

Get News and New Ticket Notices

Join our mailing list. We not only won’t spam you, but you’ll probably complain we don’t mail you enough.

Support Seattle CoderDojo

Make a $5 or $10 donation - not tax deductible.
To make a larger donation ($20 or more) that IS tax deductible, follow this link.
©2023 Seattle CoderDojo | Design: Newspaperly WordPress Theme