let’s learn JavaScript! (or melt my brain trying) – Day One

let’s learn JavaScript! (or melt my brain trying) – Day One

JavaScript syntax

  • statements (instructions executed by web browser) separated by semicolons
  • fixed values = literals
    • numbers (with or without decimals)
    • strings (text written within double or single quotes)
  • variable values = variables
    • used to store data values
    • keyword “var” used to declare them
    • assign values to variables with “=”
    • compute values with arithmetic operators (+ – * /)
  • expression = combo of values, variables and operators
    • computes to a value
    • the computation is called an evaluation
    • i.e. 6 * 10 evaluates to 60
    • values can be numbers and strings
      • “John” + ” ” + “Sausage” evaluates to “John Sausage” (string linking called concatenation)
  • keywords identify actions to be performed
    • ex. “var” tells the browser to create variables
  • identifiers = names JavaScript accepts
    • used to name variables, keywords, functions and labels
    • rules:
      • first character = a letter, underscore or dollar sign (no #s!)
      • subsequent characters = letters, digits, underscores or dollar signs
      • case sensitive (i.e. var lastName != var lastname; “VAR” is not interpreted as the keyword “var”)
      • no hyphens (reserved for subtraction operations)
    • programmers in JS tend towards “lower camel case” – camel case that starts with a lowercase letter: lastName, classicJazz, hippoFinger

Statements

  • statements are executed in the order they are written (top to bottom, left to right)
  • ending statements with semicolons = not required, just highly recommended
  • white space is ignored by browser when reading your instructions, but using it can increase readability
    • i.e. good practice = put spaces around operators
    • statement doesn’t fit on one line? break after an operator or comma
  • statements can be grouped in code blocks inside curly brackets
    • purpose = define statements that are to be executed together
    • ex: functions
  • often start with a keyword to identify what action is to be performed
    keyword description
    break terminates a switch or loop
    continue jumps out of loop and starts at top
    debugger stops execution of scripts, calls (if available) debugging function
    do … while executes a block of statements & repeats the block, while the condition is true
    for marks a block of statements to be executed, as long as condition is true
    function declares a function
    if … else marks a block of statements to be executed, depending on a condition
    return exits a function
    switch marks a block of statements to be executed, depending on different cases
    try … catch implements error handling to a block of statements
    var declares a variable
  • keywords are reserved words!! (cannot be used as names for variables)

Variables

  • used to hold data (data types in JS include numbers & strings)
    • strings = written inside double or single quotes
    • numbers = no quotes (if written in quotes, will be treated as string)
  • similar to algebra, variables are used in expressions (i.e. total = price1 + price2, where “total”, “price1” & “price2” are defined variables)
  • must be identified with unique names (“identifiers”)
  • use the “assignment” operator when declaring variables (=)
    • not to be confused with the “equal to” operator (==)
    • ex: x = x + 5 assigns the value of x + 5 to x
    • good practice = declare all variables at beginning of a script
      • can declare many variables in one statement by using commas
        • var person = “Baby Thumb”, carName = “Wristmobile”, price = “priceless”
  • a variable can be declared without a value (in that case, the value is “undefined“)
    • can be useful if value is something that needs to be calculated or provided later, like user input
    • after the execution of this statement, the variable carName will have its value undefined: var carName; ((if nothing else is added, will show up as ‘undefined’ within page))
  • can re-declare a variable without it losing its previously assigned value
    • var carName = “Wristmobile”; var carName;
  • if declare with a number in quotes, rest of numbers following will be treated as strings and concatenated (added)
    • var x = “5” + 2 + 3 (value will show as “523”)
    • var x = 5 + 2 + “3” (value will show as “73”)

Today’s Resources:

The ever-helpful W3School’s JavaScript tutorial.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s