JavaScript Basic explanations: Part 1

2/16/2021

As most know the best way to learn something is to explain it. So with this in mind I thought that while I was going over my class notes I would try to explain them!

Varriables

When we want to use JavaScript we need variables. Identifiers are what are used to name said variables. For example:

const dog = 'Han'

Dog is the name of our variable and therefore our identifier! That means that ‘Han’ is the definition of dog and const will be explained below, but there are some rules.

First of all JavaScript (JS) is case sensitive so we have to be careful when capitalizing. We also can’t use identifiers that begin with numbers. Lastly they can contain letters, numbers, underscores and dollar signs!

Let vs Const

The difference is that when using const your variables can’t be changed and let variables can be. so for example:

const dog = 'Han'

When you ask for dog you will get Han, however, if you then say:

const dog = 'Han'

dog = 'Mags'

You will now get an error because const is constant and can’t be changed.

That leads us to if you want to change your variables you use let:

let dog = 'Han'

dog = 'Mags'

This now will use Mags instead of Han because we changed the variable’s definition.

Understanding Vocabulary

Declaration of a variable: declaring a variable, but not giving it a value

Initialization of a variable: declaring a variable and giving it a value

Reassignment of a variable: giving a variable a different value

Data Types

Dynamic Programing or Loosely typed

  • You can play with data more and there are less rules
  • JavaScript, Python, Ruby

Static Programing or Strongly typed

  • More rules as to how you use data
  • C++, Swift

JavaScript has 7 ways to express data:

  • 6 primitive: Represents a single value
    • Boolean: True or false
    • Null: null
    • Undefined: undefined
    • Number: any number including infinity and not a number
    • String: anything in single or double quotes, and back ticks
    • Symbol: used for writing libraries and unique values
  • 1 Object: (also called complex or reference) is a collection of primitives
    • Examples: Array, Functions, Object, Date, ects

There are also ways to change the data

Using variable types:

let num = 18     // see  a number
num = 'Han'     // now see a string

By adding .toString or .toFixed

let num = 18.88
let num1 = num.toString     // now we get '18.88'
let num2 = num.toFixed(2)   // now we get "18.88"

Or parse

let num = 18.88
let num3 = parseInt(num)    // now we have 18
let num4 = parseFloat(num)  //MAGIC now we have 18.88

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 )

Connecting to %s

%d bloggers like this: