Section 2 Flashcards
What is a code block? What are some examples of a code block?
- denoted by curbraces
- if/else, for, do while, while, try, catch, etc
What does block scope mean?
- inside the code block
- things that happen inside the curbraces
What is the scope of a variable declared with const or let?
block-scope
- var uses global (the window global object)
- var is FUNCTION scoped
- let is not attached to the global object
- let is BLOCK-SCOPED, not initialized to any value, and not attached to the global object.
What is the difference between let and const?
- let is mutable, can be reassigned, and doesn’t have to be initialized
- const is read-only/constant, can’t be reassigned, but can be modified, is not immutable, must have initialization
- by convention, the constant identifiers are in uppercase.
const CONSTANT_NAME = value;
Why is it possible to .push() a new value into a const variable that points to an Array?
- bc we’re not reassigning a block-scope, we’re just modifying the array held by the const variable
- const’s values can change/be mutated, but it cannot be reassigned to a dif block-scope value
How should you decide on which type of declaration to use?
is the whole variable being reassigned (let), or are just the values inside the variable changing (const)
arrays & objects, always CONST
What is the syntax for writing a template literal?
backticks: this is a template literal
What is “string interpolation”?
imbedding variables & expressions in a string and JS automatically replacing them with their values
it looks like ${variable_name} this
__code read: ___
const bio = My name is ${firstName} ${lastName} and I am ${age} years old.
;
there is a template literal string with the substitutions firstName, lastName, and age, being assigned to the const bio
What is destructuring, conceptually?
- a way to assign the properties of an object to individual variables
- to get values from an object
What is the syntax for Object destructuring?
let { propertyName: variableName, propertyName: variableName } = sourceObject;
so ‘sourceObject.propertyName’ is now ‘variableName’ for ease of use
Object destructuring assigns the properties of an object to variables with the same names by default.
What is the syntax for Array destructuring?
let [var, for, each, index] = scrArray/srcFunc()
How can you tell the difference between destructuring and creating Object/Array literals?
assignment is inverted
creating, on the right
destructuring, on the left
code read:
const { title, author, libraryID } = book1;
const { title: eek, author: barba, libraryID: durkle } = book2;
the const title, author, libraryID are being destructured from book1
the const title is being aliased with eek … being destructured from boook2
What is the syntax for defining an arrow function?
parameter list, arrow, codeblock
let funcName = (parameter) => { return expression; };
When an arrow function’s body is left without curly braces, what changes in its functionality?
- it will return automatically
- implicit/implied return = withOUT curbraces
- explicit return = WITH cubraces
How is the value of THIS determined within an arrow function?
- from the enclosing lexical scope. so you cannot use a new this in an arrow function, unless you:
- assign the this to a variable, then use that variable in your arrow function
- this in an arrow func captures this’s value from the enclosing context instead of creating its own
What is a CLI?
command line interface
What is a GUI?
graphical user interface
Give at least one use case for each of the commands listed in this exercise.
man – interface to the online reference manuals
cat – concat files & print to standard output, but rly for viewing file contents
_cat filename -quickly see the contents of a file
ls – list directories
pwd – print working directory
echo – display a line of text
touch – change file timestamps, but rly for creating empty files
_touch empty-file.txt -create an epmty file
mkdir – make directories
mv – move (rename) files
rm – remove files/directories
cp – copy files/directories
What are the three virtues of a great programmer?
- Laziness: The quality that makes you go to great effort to reduce overall energy expenditure. It makes you write labor-saving programs that other people will find useful and document what you wrote so you don’t have to answer so many questions about it.
- Impatience: The anger you feel when the computer is being lazy. This makes you write programs that don’t just react to your needs, but actually anticipate them. Or at least pretend to.
- Hubris: The quality that makes you write (and maintain) programs that other people won’t want to say bad things about.
What is Node.js?
- a js environment outside of a browser
- can be used to build a server
a program that allows JavaScript to be run outside of a web browser, especially for talking/building to backend/servers/etc as a foundation for a web application
What can Node.js be used for?
building servers or backends for web applications
What is a REPL?
Read–eval–print loop
A read–eval–print loop (REPL), also termed an interactive toplevel or language shell, is a simple interactive computer programming environment that takes single user inputs, executes them, and returns the result to the user.
What is the process object in a Node.js program?
- a global variable that gives info about the current node.js process
The process object is a global that provides information about, and control over, the current Node.js process.
How do you access the process object in a Node.js program?
- same way you would any object ?
- through node in the terminal
- through node in the terminal executing js file with a console.log of the object/properties
As a global, it is always available to Node.js applications without using require(). It can also be explicitly accessed using require(): const process = require('process');
What is the data type of process.argv in Node.js?
- an array of strings
What is a JavaScript module?
- a single .js file, full of code that interacts with other modules full of code to create a cohesive application
What values are passed into a Node.js module’s local scope?
- exports
- require
- module
- __filename
- __dirname
Give two examples of truly global variables in a Node.js program.
- console
- setinterval
- settimeout
- process
What is the purpose of module.exports in a Node.js module?
- insures that the code in the module is able to be seen & accessed (connected to) by other modules in the application
How do you import functionality into a Node.js module from another Node.js module?
- with require()
What is the JavaScript Event Loop?
- js’s logical ordering of what it’s supposed to do
- a continual loop of checking the call stack and executing functions
What is different between “blocking” and “non-blocking” with respect to how code is executed?
- whether or not a process impedes other processes
- non-blocking will offset that stuff to do other things until it’s the slow thing is ready
- blocking would allow the slow thing to impede otehr things
The Event Loop is one of the four major concepts that sets JavaScript apart from many other languages.
- Prototypal Inheritance
- how this works
- closures
- the event loop
Understanding how asynchronous programming works is absolutely critical to modern Web development and especially programming in JavaScript.
What is a directory?
a folder that holds files/a repository
What is a relative file path?
relative to where you are locally
What is an absolute file path?
the full path on the system, relative to root
What module does Node.js include for manipulating the file system?
fs (file system)
What method is available in the Node.js fs module for writing data to a file?
.writeFile() method
Are file operations using the fs module synchronous or asynchronous?
asynchronous
What is a client?
- a requester of a service or resource provided by a server
What is a server?
- a provider of a service or resource
Which HTTP method does a browser issue to a web server when you visit a URL?
GET
What is on the first line of an HTTP request message?
- an HTTP method; GET, PUT, POST, etc
- the request target; URL or absolute path, port etc
- the HTTP version
A start-line describing the requests to be implemented. This start-line is always a single line.
What is on the first line of an HTTP response message?
- the protocol version, usually HTTP/1.1
- a status code indicating success/failure; 404, 200 etc
- a status text, brief info for the humans
A status-line describing whether successful or a failure. This start-line is always a single line.
What are HTTP headers?
optional specification info
Is a body required for a valid HTTP message?
nope. optional
What is NPM?
- Node Package Manager
- a way to re-use code from other devs, or share your code for use
- consists of:
- website, use to discover packages, set up profiles, and manage other aspects of your npm experience.
- CLI, runs from a terminal, and is how most developers interact with npm.
- registry, a large public database of JavaScript software and the meta-information surrounding it.
What is a package?
- bits of reusable code, also called modules
- a directory with files that also has package.json
How can you create a package.json with npm?
- make sure you’re in the correct directory, then:
- npm init –yes
What is a dependency and how to you add one to a package?
- a connection to a library of code your program will access/use
- npm install ‘package-name’
- npm uninstall ‘package-name’
What happens when you add a dependency to a package with npm?
- it’s installed into the node-modules folder in the directory where it’s created
- it’s added to the dependencies object
How do you add express to your package dependencies?
- be in the correct directory
- npm init –yes to make a new package.json to hold it
- npm install express
What Express application method starts the server and binds it to a network PORT?
- listen() method