General Assembly

The Command Line & Data Types

Wilson Espina

Review

What did we do last lesson?

The Command Line

Learning Objective

Learning Objectives

At the end of this class, you will be able to:

  • Work with files/directories via the Terminal
  • Introduce Git basic commands and distinguish Git from GitHub
  • Run basic JavaScript code on the command line interface (CLI)
  • Introduction to variables and primitive data types

The Command Line

Agenda

  • Using the Terminal
  • Git and GitHub
  • Command Line JS
  • Variables & Data types

The Command Line

Exit Ticket Questions

  1. Can we have a lesson plan in advance?
  2. More help installing things

The Command Line & Data Types

The Terminal

The Command Line & Data Types

Javascript on the Command Line

  • Terminal===Command Line===Console
  • Terminal allows you to interact with your computer faster
  • Lets you perform tasks that would be difficult through graphical user interface (GUI)
Command Line Interface

The Command Line & Data Types

Shell

  • A generic name for the primary program that runs inside a Terminal
  • Common shell programs include:
    • Bash (Bourne-Again Shell)
    • ZSH (Z Shell)
    • Powershell
Shell

BONUS: Type like a Hacker

The Command Line & Data Types

Anatomy of the Terminal

SPACE

Command Line

The Command Line & Data Types

Anatomy of the Terminal

SPACE

Command Line

The Command Line & Data Types

Anatomy of the Terminal

SPACE

Command Line

The Command Line & Data Types

Anatomy of the Terminal

Command (program)

Command Line

The Command Line & Data Types

Anatomy of the Terminal

Flag or Option

Command Line

The Command Line & Data Types

Anatomy of the Terminal

Output

Command Line

Code along

Code Along

Let's get to grips with some Terminal commands

Activity

Exercise

Key Objective

  • Begin to use basic Terminal commands

Type of Exercise

  • Individual

Timing

15 mins

  1. Navigate to the desktop
  2. Create a directory called films
  3. Go into this directory
  4. Create a file for your favourite film called (FILM NAME).txt
  5. Open this file in VS Code and add some text inside
  6. Create another 3 files for other films in one line
  7. Rename one of the films to the name of the sequel
  8. Open the entire directory inside VS Code so you can see all files and make some changes (remember to save files)
  9. Delete your two least favourite films

Activity

Exercise

Key Objective

  • Begin to use basic Terminal commands

Type of Exercise

  • Individual

Timing

10 mins

  1. Follow the instructions posted on the class web portal to navigate and modify files and directories using the command line.

The Command Line & Data Types

Introduction to Git / GitHub

The Command Line & Data Types

Git

Git is distributed version control system

  • Programmers use Git to keep the history of all the changes to their code
  • This means that they can rollback changes (or switch to older versions) as far back in time as they started using Git on their project.
Git logo

The Command Line & Data Types

GitHub is for collaboration

  • A hosting service for Git repositories
  • A web interface to explore Git repositories
  • Hosts files in the cloud.
  • A social network of programmers
    • We all have individual accounts and put our codebases on our GitHub account
    • You can follow users and star your favorite projects
Git logo

The Command Line & Data Types

Git / GitHub Key Terms

Git repository
  • Repository (aka repo) - Contains all of a project's files (all the code)
  • Clone - Git command that copies/clones a remote repo to your machine
  • Commit - A snapshot that has been saved to the project history
  • Push - Git command that sends your commits (saved changes) to a remote repository
  • Pull - Git command that copies (pulls) changes by other developers from a remote repository to your local clone

The Command Line & Data Types

Local vs Remote

Local

Laptop
  • Repository on your own machine
  • Similar to saving on MS Word

Remote

Cloud computing
  • Code is stored in the Cloud and is accessible by others
  • Similar to saving on Google Drive

The Command Line & Data Types

How We Will Use GitHub in JS9

Folder

jsd-9-resources

  • contains start and solution files
  • you will pull changes at the start of each lesson
Folder

jsd-9-homework

  • you will push your completed homework and receive feedback here
Folder
  • you will create additional repos for your own projects

The Command Line & Data Types

Git Commands


git init
						
  • Initiates a new local Git repository
  • Creates a .git subdirectory in the current working directory

The Command Line & Data Types

Git Commands


git add -A
git add .
						

Or for individual files


git add filename
						
  • Adds or stages changes in the working directory to the files to be included in the commit snapshot.
  • This snapshot is now stored in a temporary staging area which Git calls the "index".

The Command Line & Data Types

Git Commands


git commit
git commit -m "Commit Message"
						
  • Permanently stores the contents of the index in the repository.
  • Commits the staged files to the project history as a snapshot.
  • Also adds a timestamp and records who made the changes.

The Command Line & Data Types

Git Commands


git branch newbranchname
git checkout -b newbranchname
						
  • Makes a copy of the working directory in the current branch and allows you to make changes without affect the other branch.
  • The git checkout –b command is a shortcut to create the branch and move into it.
  • Use git checkout branchname, to return to an existing branch.

The Command Line & Data Types

Git Workflow

Git workflow diagram

Code along

Code Along

Let's try out Git Commands

The Command Line & Data Types

Node js

The Command Line & Data Types

Node.js

  • Node.js is an open-source, cross-platform runtime environment for developing server-side Web applications.
  • Interprets JS using Chrome's V8 engine
  • Faster execution with non-blocking, asychronous input/output
  • Written in C, C++, and JS (so, not a JS framework)
Node js logo

The Command Line & Data Types

Levels of Abstraction

Iceburg Iceburg
  • 1's and 0's towards the bottom of the iceburg
  • Human readable languages towards the top
  • C++ is lower level
  • JavaScript is a high level of abstraction from machine code

The Command Line & Data Types

Node vs the Browser

Node Browser
  • Can interact with the local OS and filesystem
  • The Browser cannot affect the OS
  • Has no native ability to manipulate web pages.
  • Frequently used to manipulate web pages via the DOM (Document Object Model)
  • Headless (without a GUI)
  • User interface
  • Everything is written inside modules
  • No requirement for use of modules

The Command Line & Data Types

What is Node used for

Swiss army knife
  • Allows us to run JavaScript from our terminal applications
  • Creating and running scripts
  • File management
  • Creating a backend server for a web application

The Command Line & Data Types

Running Node in the Command Line

Interactive command line

  • To enter mode type: node at the command prompt.
  • To exit type: .exit or press CTRL + C twice

	> 3 + 4 // your command
	< 7 // Node's response
								

Running a file

  • Run a file by using node and the filepath as the first argument:

	> node /filepath/script.js
								
  • Node loads the script.js file and executes the contents

	> 7
								

The Command Line & Data Types

JavaScript Variables

The Command Line & Data Types

Variables

Containers for data that allow us to store values

  • Allows our program to remember values for us to use later on
  • The action of saving a value to a variable is called assignment
  • Similar to putting information on a shelf
Jar

The Command Line & Data Types

Declare the Variable


let age;
							

The Command Line & Data Types

Assign a value


let age = 32;
							

The Command Line & Data Types

Declare and assign a value


let age = 32;
const age = 32;
var age = 32;
							

The Command Line & Data Types

Naming Variables

Camelcase
  • Use camelCase - first letter is lowercase and proceeding words are uppercase: anExampleOfCamelCase
  • Names can only contain: letters, numbers, $ and _
  • No dashes, no full stops and cannot start with a number
  • Case sensitive - numberofbooks is not the same as numberOfBooks

The Command Line & Data Types

Printing things out for inspection


console.log(age)
							

The Command Line & Data Types

When do you use console.log

  • Used only during development to check what's going on
  • Allows you to debug your code
  • You can add multiple console.log's

Code along

Code Along
Code Along

The Command Line & Data Types

Data Types

The Command Line & Data Types

Primitive Data Types

A data type provided by a programming language as a basic building block. Examples in JavaScript are:

  • String"All strings are wrapped in quotations"
  • Number15
  • Booleantrue

The Command Line & Data Types

Strings

  • They are sequences of Unicode characters
  • Can be declared with either double quotes " or single quotes '
  • They can be stored in variables
  • They can be manipulated with methods like .toUpperCase()

let title = "star wars"

title.toUpperCase();
"STAR WARS"
							

The Command Line & Data Types

Numbers

  • They can be stored in variables
  • You can perform arithmetic operations on them
  • There's no such thing as an integer in JavaScript, so you have to be a little careful with your arithmetic if you're used to maths in C or Java.
  • Can lead to odd behaviour:

> 0.1 + 0.2
0.30000000000000004
							

BONUS: The reason why 0.1 + 0.2 does not equal 0.3

The Command Line & Data Types

Arithmetic Operators

The standard arithmetic operators are supported, including addition, subtraction & modulus (or remainder)


> 1 + 2;
3

> 2 - 5;
-3

> 5 / 2;
2.5

> 6 * 2;
12
						

The Command Line & Data Types

Compound Operators

+=

adds a number to a variable and assigns the new value to the same variable

-=

subtracts a number from a variable and assigns the new value to the same variable

++

adds 1 to a value

--

subtracts 1 from a value

The Command Line & Data Types

Modulus (%)


> 9 % 4
1
						
Modulus

The Command Line & Data Types

Special Number Operators

The Math object provides methods for additional operations


Math.pow(m,n)	// Returns m to the power of n
Math.sqrt(n)	// Returns the square root of n
Math.random()	// Returns a random number between 0 (inclusive) and 1 (exclusive)
Math.floor(n)	// Returns largest integer less than or equal to n
Math.ceil(n) 	// Returns smallest integer greater than or equal to n
						
Calculator

The Command Line & Data Types

Boolean

A Boolean value can either be true or false. Any value can be converted to a boolean value in JS

Activity

Exercise

Key Objective

  • Describe the concept of a "data type" and how it relates to variables.

Type of Exercise

  • Discussion in pairs
  • Add comments to the Slack threads

Timing

3 mins

  1. Describe variables. Explain why we would want to use variables in our programs
  2. Name three data types in JS. Can you think of an example of each?

Intro

Learning Objectives - Review

  • Work with files/directories via the Terminal
  • Introduce Git basic commands and distinguish Git from GitHub
  • Run basic JavaScript code on the command line interface (CLI)
  • Introduction to variables and primitive data types

Intro

Look Ahead to Next Lesson

  • Running JavaScript in the Browser
  • Create arrays and access values in them
  • Build iterative loops using for statements.
  • Iterate over and manipulate values in an array.

The Command Line & Data Types

Q&A

The Command Line & Data Types

Homework

The Command Line & Data Types

Exit Ticket

(Lesson 02)