JS Fundamentals Flashcards
Retaining fundamental javascript concepts, terminology, and theory (27 cards)
What is ‘scope’ in javascript?
In JavaScript, each function gets its own scope.
There are two scopes in js - global and local. Variables declared outside of a function have a global scope. Variables declared within a function have a scope local to that function.
Scope is a collection of variables and the rules for how those variables are accessed by name.
Only code inside that function can access that function’s scoped variables.
A variable name has to be unique within the same scope.
A scope can be nested inside another scope.
If one scope is nested inside another, code inside the innermost scope can access variables from either scope.
What is ‘equality’ in javascript?
JavaScript has both strict and type–converting comparisons:
Strict comparison (e.g., ===) checks for value equality without allowing coercion. It is looking for absolute equality. Both values must be the same type to return true equality. Abstract comparison (e.g. ==) checks for value equality with coercion allowed. The equality operator converts the operands to a similar type if they are not of the same type, then applies strict comparison.
What is ‘typeof’ operator?
Examines a value and tells you what type it is. E.g.:
undefined string number boolean object
What are arrays in javascript?
An object that holds numerically indexed values of any type.
Explain values and types in javascript.
Javascript has types values, not types variables. The values are as follows:
string number boolean null and undefined object symbol (new to ES6)
Explain null and undefined in javascript.
Undefined: A variable type where something has been declared but assigned no value - something hasn’t been initialized. Indicates variable points to no object (everything in javascript is an object).
Null: An object value that signifies nonexistence of any value - something is currently unavailable.
What is ‘strict’ mode?
Feature introduced in ES5.
Allows you to place a program, or function, in operating context that prevents certain actions from being taken and throws more exceptions. E.g. does not allow for undeclared variables.
Declared as string ‘use strict’ in the beginning of script. Has global scope. If declared inside function, has local scope to function.
Explain the ‘this’ keyword in javascript.
Always returns a reference to the current object.
If called on a standalone function outside of an object, returns a reference to the global object (which in browsers is the window object). If strict mode is enabled, returns undefined.
The above problem is fixed by binding. Using the bind method binds a function to an object.
What are arrow functions in javascript? When should we use them?
=>
Introduced in ES6
Saves time, cleaner more compact syntax by removing ‘function’ and ‘return’ from function declarations.
**They also do not rebind ‘this’, they INHERIT the this keyword, therefore no need to use .bind and super method in react
What is .map?
A function introduced in ES6 that returns a new array based on instructions applied to values inside an array.
In react it is used to render lists.
What is object destructuring?
Extracts properties from objects and stores them in variables. E.g.
const { street, city, country } = address;
The properties street, city, and country in the address object are now stored as their own respective variables and can be called on an object to return that value.
What is an object in javascript?
A collection of key/value pairs.
Functions inside objects are referred to as methods.
Explain let vs var vs const
Represent values through binding
Var: global variable scopes to the function. Available outside of the block it is declared in within the function.
Let: only available within block declared in. Scoped to block. Value can be reassigned.
Const: scoped to block. Variable that cannot be reassigned.
What is the spread operator in javascript?
Represented by syntax ‘…’
Contains all values within an object. Used to combine arrays.
E.g. const first = [1, 2, 3] const second = [4, 5, 6] const combined = [...first, ...second]
What are classes in javascript?
An object that provides a blueprint for an object of that type.
Use the constructor method (special keyword with name reserved) which accepts parameters that make up the properties for that class.
Syntax is class Object (uses capitalization)
What is inheritance in javascript?
Classes can inherit properties and methods from other classes by writing ‘extends’ and the name of the class they are inheriting from.
The class being inherited is the parent class, and that class that is inheriting from the parent class is the child class.
If child class requires more parameters, we can use the super method to initialize them.
How would you empty an array in javascript?
array. length = 0
array. splice(0, array.length)
array = [];
What is a callback function?
A function that is passed to another function as one if its arguments/parameters.
What language constructions can we use for iterating over objects and arrays?
For objects:
- for loops (also iterates over inherited properties)
- object.keys() (static method that lists all enumerable properties of the object you pass to it)
- object.getOwnPropertyNames() (static method that lists all enumerable and non-enumerable properties of the object passed to it)
For arrays:
- for loops (allows for more flexibility, like prematurely terminating loop with ‘break’)
- forEach()
What is the ‘load’ event? Why would you use it (advantages, disadvantages)
Fires at the end of the document loading process. All objects in document are now in the DOM, images, scripts, links, and sub-frames have finished loaded.
DOMContentLoaded fires after the DOM has been constructed, but does not wait for other resources to finish loading. Preferred in cases where you do not need the full page to be loaded before initializing.
What is AJAX?
Asynchronous JavaScript XML
What is XML? Stands for Extensible Markup Language. Markup languages are coding languages used to annotate parts of a web document that are intended to give web browsers instructions about how to understand, process, and display a web page, versus the actual text intended to be displayed on the page. XML is used to transfer data stored on the page to the browsers that view it. Individual computer systems are often incompatible with one another and can’t understand or interact with data formatted by a different system. XML allows developers to bypass this obstacle by storing data in plain text format between XML tags.
Read data from a web server - after a web page has loaded
Update a web page without reloading the page
Send data to a web server - in the background
AJAX’s core function is to update web content asynchronously (the “A” of AJAX), meaning a user’s web browser doesn’t need to reload an entire web page when only a small portion of content on the page needs to change.
AJAX functions by creating a XMLHttpRequest Object. When a user visits a webpage that makes use of javascript, javascript creates this object and then XML transfers the data from the browser to the server. The XMLHttpRequest object sends a request for updated page data to the web server, the server process the request, a response is created server-side and sent back to the browser, which then uses JavaScript to process the response and display it on the screen as updated content.
Used often to integrate third-party APIs into app functionality or fetching JSON data.
Also used to update forms and perform other interactive javascript functions, like buttons, scrolling, animations, etc.
What is JavaScript?
JavaScript is the Programming Language for the Web
A language read by the browser
JavaScript can update and change both HTML and CSS
JavaScript can calculate, manipulate and validate data
Is asynchronous, which is critical to the functionality of javascript
after markup languages like HTML and CSS are used to build and display static web features (headers, fonts, paragraphs, etc.), JavaScript is then used to control features that require real time updates while a visitor is viewing a page (think interactive maps, animated graphics, scrolling video, jukeboxes, etc).
What is ES6?
ECMASCRIPT are the specifications that define JS
ES6 AKA ES2015
Created to standardize JA
Scripting-language specification
Commonly used for client-side
Scripting for WWW and for server applications such as node.js
Popular changes introduced through ES6:
let & const
.map
Explain the difference between == and ===
== looks for equivalence (equality via coercion) === looks for exact equality (no coercion of value types_