JS: Basics

There are 6 data types in JavaScript:

These are referred to as primitives i.e. they are what they are, you can't manipulate them the way you can their object counterparts (below.)

What if we want to create a string using statically typed text and variables together?

There are two ways:

  • 
        var myName = "Ryeker";
    
        alert('Hello, ' + myName + '!'); //ES5
    
        alert(`Hello, ${myName}!`); //ES6
                      

We will be using ES6-style string interpolation.

In order to understand the rest of this day's examples, you'll need to grasp the concept of a "block" of code.

  • Blocks are little more than grouped code that (almost always) forms something called scope.
  • We will deep dive into scope a future class, but for now, understand that scope is like a boundary that limits your JS code to one area.

var, let, const: the variable types

I mentioned on day 1 that JavaScript is "Object Oriented." But what does that mean?

  • Everything in JS is an "object", or a series of pre-defined, easily manipulatable descriptions of something.
  • We will dive deep on this subject in-depth in week 5, but for now, let's take a look at some predefined objects in JavaScript
  • Date
  • new Date()
  • Math
  • Give me a random integer between 1 and 1000!
  • Math.PI()
  • String
  • new String()
  • Check the length of that string
  • Number
  • Number('0o11')
  • Boolean
  • What a bunch of bool...
  • **Almost always use the primitive form of Boolean...
  • Arrays
  • new Array('apple', 42, 'Yoda');
  • Object
  • Click to know truth!
  • don't confuse primitives with their JS object counter-parts!

There are many ways to interact with your users. Here are the basic 3:

  • alert(): Simply gives the user a message
  • confirm(): Presents the user with a choice and returns a Boolean
  • prompt(): Presents the user with a question and returns the user's input

These are all functions. What's that?

We will get much deeper into functions in the following class sessions, but for now, understand a function is nothing more than some grouped JavaScript behavior that performs a specific set of actions. You call a function. It returns a value.

  1. On a new branch called takehome-day2, Write a program that:
    • Asks for the user's first name
    • Asks for the user's last name
    • Logs the user's first name to the console
    • Alerts the user's last name
    • *Asks for the user's birthday, confirms whether the birthday recieved is correct, and alerts the resulting birthday
  2. Merge your takehome-day2 branch with gh-pages so I can test it
  3. Send me the url of the gh-pages on Slack

* Extra points if you use JavaScript's Date object for the birthday.