JS Notes

Variables

Creating variables

You create a variable with the keyword var followed by a name for your variable. You can then assign a value to your variable with an equals sign (=) followed by the value you want to give it.
  • Variable var keyword
  • Name of variable
  • Assign value with =
  • Value you want to give

Example:

var randomNumber = Math.floor(Math.random() *100) +1;

In the last example, var just states "I'm creating a new variable", randomNumber is setting a name to the variable, the = is to define what the value of that variable is going to be and the next piece of code is the value itself.

Functions

Functions are reusable blocks of code that you can write once and run again and again, saving the need to keep repeating code all the time. This is really useful. There are a number of ways to define functions, but for now we'll concentrate on one simple type. Here we have defined a function by using the keyword function, followed by a name, with parentheses put after it. After that we put two curly braces ({ }). Inside the curly braces goes all the code that we want to run whenever we call the function.

Creating a function

  • function keyword
  • nameOfTheFunction() Name of the function followed by open and close brackets
  • The actual function inside {}

 

Questions JS.01

At this point, after learning how to "create" a variable and a function, here are some questions that I have.

  • When do I use a function?
  • What are the multiple pieces of a function?
  • What is the typical structure I need to feel in of a "program" using variables and functions?
  • Are variables and functions the only foundational pieces?
  • What other pieces do I have to start building up my program?
  • What are the relationship between variables and functions?

Operators

They're mostly math operators (+,-,*,/). They work both in the console and in the code.

You can also use the + operator to join text strings together (called concatenation in programming).
There are also some shortcut operators available, called augmented assignment operators. 
nameOfTheVariable += 'some string here';
This is equivalent to

nameOfTheVariable = nameOfTheVariable + 'some string here';

Operator Name Example

  • === Strict equality (is it exactly the same?)
    • 5 === 2 + 4
  • !== Non-equality (is it not the same?
    • 'Chris' !== 'Ch' + 'ris'
  • < Less than
    • 10 < 6
  • > Greater than
    • 10 > 20

Conditional

A conditional looks look a function, but it isn't.


A conditional code block allows you to run code selectively, depending on whether a certain condition is true or not. It looks a bit like a function, but it isn't. The simplest form of conditional block starts with the keyword if, then some parentheses, then some curly braces. Inside the parentheses we include a test. If the test returns true, we run the code inside the curly braces. If not, we don't, and move on to the next bit of code.

  • if
  • (test goes here)
  • {code to run if test is true}

 

Questions JS.02

  • Can I use " instead of '?
  • What's the difference between working at the console level and a file level?
  • Why does the console returns "undefined"?
  • Why can a variable be declared inside of a function?
  • When does a variable is lives outside or inside of a function?
  • What's the structure of a function?
  • What are the canonical examples of a function?
  • How do I have access to HTML/CSS properties?
  • When do I need to write something next to parenthesis likeThis()? When is it necessary?
  • What does () mean?
  • What are the in built methods that I have access? How can I know all these methods, how to use them, and in which situations?

Events

Events are actions that happen in the browser, like a button being clicked, or a page loading, or a video playing, in response to which we can run blocks of code. The constructs that listen out for the event happening are called event listeners, and the blocks of code run in response to the event firing are called event handlers.

Some text....