JavaScript Quiz Questions Flashcards
What is a variable?
Variables store data
Why are variables useful?
They are reusable and they can be reassigned later. They are also useful for readability
What two special characters can a variable begin with?
$ and _
How do you declare a variable?
By using the word var and giving it a name
How do you assign a value to a variable?
A variable is assigned with a “=”, which is a n assignment operator
Are variables case sensitive?
Yes
Which words cannot be used as variable names?
Keywords such as “var”, “function”, “true”, etc..
What is a string?
Data stored in ‘ ‘ or “ “
What is the string concatenation operator?
The addition operator/ +
What is the difference when it comes to using single quotes or double quotes ( ‘ ‘ or “ “ )?
There is no difference
How do you escape quotation characters?
With a backward slash \
What is type coercion?
When JavaScript converts data types behind the scenes to complete an operation
What is a number in JavaScript?
Numeric data type (primitive data type)
What is an arithmetic operator?
An operator that performs basic math
Name four of the arithmetic operators
+, -, *, /, –, ++, %
What is the order of execution?
Multiplication and division are performed before addition or subtraction. Parentheses overpowers everything.
What is a boolean?
A data type that only returns the values true or false
What is a comparison operator?
Compares two values and returns true or false
What is the difference between undefined and null?
The difference is when you declare a variable without giving it any value, the data type of that variable is undefined. Null on the other hand must be assigned programmatically. It represents the absence of value and usually a variable that is null, will have a value later on in the program.
Why is null an object?
It’s a bug
How do you think building this layout would be different without a grid system?
It would be a lot of elements with different sizings
What advantages do you see with using a grid system?
Helps size things quickly, readability, and for mobile responsiveness
Why are media queries crucial to responsive grid designs?
It helps with mobile responsiveness/ resizing for smaller or bigger screens
What is a function?
A reusable block of code with a series of statements grouped together to perform a specific task.
Why are functions useful?
It saves time from writing repetitive code
How do you call a function?
By writing the function name with the parentheses
What are the parts of a function definition?
The function keyword, the function name, the parentheses with the parameters, and the function code block
What is the difference between a parameter and an argument?
Parameter is the name of the variable. Argument is the value of the variable being passed into the function
Why is it important to understand truthy and falsy values?
To understand possible type coercion errors
Why do you want to avoid using == for comparison?
Type coercion can lead to errors
Do all if statements require an else statement?
No
What is the proper syntax for using the or operator?
if ( var1 < var2 || var1 > var2)
What is the primary use case for switches?
For direct 1 to 1 comparisons
Does the default case have to be at the bottom of the switch statement?
No
What happens if there is no break statement between cases?
The next case will be executed even if the evaluation does not match the case
What is an object in JavaScript?
An object is a container for a group of properties and values
How do you create an object literal?
var objectName = {}
What is a property in relation to JavaScript objects?
A property in relation to objects is a key value pair
When should you use bracket notation over dot notation with objects?
When substituting a property or if there are spaces/special characters in a property name
How do you remove a property from an object?
Use the delete function:
delete objName.propertyName
What is an array in JavaScript?
A list of data with order/indexs
How do you create an array literal?
var arrayName = []
What are the keys for the values inside an array?
Index numbers and values
Arrays have a property named length. Because arrays have a properties, what other data structure are they similar to?
Objects literals
What are some good use cases for using objects inside arrays?
Organization and more data
What is the difference between the getElementById() method and the querySelector() method?
queryselector targets a css selector where as the getElementById only grabs elements Id’s
What is the primary advantage to storing your selected elements in a variable?
To reuse that element rather than searching for it on the dom over and over again
Why might you need JavaScript to modify the DOM after the page loads?
In order to update a document more efficiently and as new situations arise, we can alter that document rather than create a brand new one
How can you better prepare when writing HTML for DOM manipulation in your JavaScript code?
Create ids and classes
What are the differences between innertext and textContent?
innerText get the computed text and textContent gets the entire text
What datatype are the values you remove from a text input?
‘string’
Why is it so important to be able to dynamically update text?
You would have a static website with text that doesn’t change
What are some of the drawbacks of HTML Event Handler Attributes?
It’s better to separate html from javascript
Why is the Window.event property useful for finding an element which was interacted with?
its helpful because it has a reference to which ever element that event occurred on within the object
Why is the Window.event property to be avoided in new code?
Its not dependable for being the same object that you want for the event that you’re dealing with
What potential use cases are there for for loops?
For counting down
Which pieces of information provided in the parentheses for a for loop are mandatory?
They are all optional, but you should still use all of the information (Initialization, condition, and update)
Which pieces of information provided in the parentheses for a for loop are mandatory?
They are all optional, but you should still use all of the information (Initialization, condition, and final expression)
What is a for in loop?
A loop that iterates through objects and it properties
How do you target the value of a property in an object.
bracket notation within the for in loop
When should you use a for in loop?
When you want to display the data inside an object
What kind of information is useful to store in custom attributes?
Semantic info
What is the difference between the parentNode and parentElement properties?
The only difference is that the html element has a parent node of document, but does not have a parent element.
Why is it important to be able to traverse the DOM?
When you are unable to create a class for an element (Very rare situatuion)
What are two ways to target elements on the DOM?
queryselector and getElement
What is another way to add a text node to an element other than using textContent.
createTextNode and then appendChild to the element
How do you create a HTML element using vanilla Javascript?
createElement method
Why is using loops and arrays for creating multiple dom elements preferable to creating them one at a time?
To not have to repeat the code over and over. It cuts time down.
Why are arrays preferred over objects for the functionality referenced in question 1?
An array is indexed which makes it easier to loop over
Why is it important to be able to retrieve and use data from inputs?
It is important to handle user input data
Why is it dangerous to assume you have the correct data when creating elements?
You can’t control what people input and can cause problems with your page
Why is it dangerous to assume you have the correct data when creating elements?
You can’t control what users input and can cause problems with your page, such as hacking.
How would you alter the game to make the choice from 1 - 500?
Math.floor((Math.random() * 100) + 1)
Change the 100 to 500
What are the disadvantages of inline styling via JavaScript?
Inline script cannot be tested independently of its page; external Javascript files can be run through independent testing, including automated tests
What things do you have to consider when building a reset game function?
The initial starting values of everything at the beginning of the game
What is event bubbling?
When event listener is called from the element that was triggered and it moves back up through its parents through to the dom
What is the difference between event.target and event.currentTarget.
Event target is the element that was initially triggered and currentTarget is the element that has the event listener on it
What is the first argument the function setTimeout takes?
function
If the second argument is not passed into the setTimeout function, what is the default value?
0
What are some scenarios where setTimeout can be useful?
For a modal that might pop up to ask if you’re still there
What does the setInterval function return?
It returns a number
What argument does the clearInterval function take?
The setInterval ID
What are some scenarios where setInterval can be useful?
Timers
What is the event loop?
It is responsible for looking at the task queue and pushing events into it from the call stack
What does the term blocking mean?
When the call stack is full and other events can’t be pushed into it
What is the call stack?
The current workflow of tasks flowing through Javascript
Which elements in a website are useful to create dynamically?
Ex: Products on an retail website
Why should you not create all elements dynamically?
Some elements should be static in html
Why do we need a single source of truth?
To keep track of where to store data
Why would I create a function to generate my DOM Element instead of just writing my code in the global scope?
It’s more reusable and versatile
What is a method?
It’s an object function
What does a method do?
Retrieve or update the values of an object’s properties
What is this?
A keyword that grabs a property’s value of an object
What does bind do?
It binds a permanent argument to a new copied function
What is the difference between a function and an object literal?
functions are callable objects, but they have three methods (apply, bind, call)
What is Prototypal Inheritance?
Inheritance of methods from the proto object
What is the Prototypal Chain?
A chain that tries to find a method and if it can’t find the method, it will go all the way up through other objects until it gets to the global object to find a method. It will stop once the method is found.
In the custom objects we created, I noticed that our prototype object has another proto property, where did that come from?
From the global Object
Why does JavaScript have Prototypal Inheritance?
Efficiency in memory
What does the new keyword do?
It creates an instance of a user-defined object type or of one of the built-in object types that has a constructor function
Also
Creates a blank, plain JavaScript object;
Links (sets the constructor of) this object to another object;
Passes the newly created object from Step 1 as the this context;
Returns this if the function doesn’t return an object.
I could’ve added a method to the constructor function by assigning a method to a property on the this object. Why do we have to add it to the prototype property?
It’s bad for memory efficiency
What is the first thing that happens in a class when it is instantiated with the new keyword?
An empty object is created
Since classes are technically functions as well. Can you name a difference between classes and functions?
Classes will always use the new keyword where as functions can use new, but not always
What is a static method?
Methods that can be used without creating an instance. It is also attached to the creator/ prototype
What is the benefit of instantiating Classes within other classes?
So that classes can hold other classes
Why are parent - child relationships important in OOP?
To create connections
What is the basic idea of OOP?
Organizations and keeping related things together
Why did you have to bind this for the feedChild method in the Parent class?
If we didn’t bind it, the “this” would have tried to find the “this.food” within class Child instead of looking for it in the class Parent
Why is it important to assign callback methods to properties in the constructor?
So that it is defined and can be called later
Why did the Maker class require all of the Parent class info?
So that it could pass in data into a nested class
Why did you have to bind this for the replenishFood method?
To look for data in maker
What decides the order in which JS files must be loaded?
The data being accessed needs to exist before you can access it
What is JSON?
A text-based data format following JavaScript object syntax
What are serialization and deserialization and why are they useful?
Serialization is the process of turning an object in memory into a stream of bytes so you can do stuff like store it on disk or send it over the network.
Deserialization is the reverse process: turning a stream of bytes into an object in memory.
How to you serialize data into a JSON string using JavaScript?
stringify method
How do you deserialize a JSON string using JavaScript?
parse method
Variables defined with the var keyword are function scoped. const and let are _____ scoped.
block
Name some characteristics of a variable declared using let.
mutable, block-scope, not hoisted
What does block scope mean?
The scope within the current block of code
In the file we can push a new value to an array defined using the const variable. How does that work?
We are not changing the value of the variable, just the value within the array
Name some things that var can do that const and let can’t.
var can be hoisted, var becomes a property of the window obj, var can be reassigned
If var can be used virtually anywhere, why would we switch?
const and let leave less room for error. Var can do things in your code that you don’t want it too
Name some characteristics of a variable declared using const.
immutable, block-scope, not hoisted
What is the syntax in writing a template literal?
${javascript expression}
What is string interpolation?
A way to inject javascript into a string without concatenation
When was destructuring introduced?
In 2015 with es6
Why could the running commas technique of destructuring an array be a bad idea?
It can be easy to lose track of the count of commas
How can you easily tell the difference between destructuring and creating array/object literals.
The placement of the object literal (left or right)
How does an ES6 arrow function differ from an ES5 function?
arguments, super, this, and new.target are lexical in arrow functions and arrow functions can’t be constructors.
When an arrow function’s code block is left without curly braces, what (if anything) changes in its functionality?
Nothing
In what situations would you possibly use an arrow function?
When lexical scope is needed
In what situations would you not want to use an arrow function?
When you don’t want to use lexical this
What is Array filter useful for?
creates a new array of filtered items
What is Array map useful for?
creates a new array of items that have their values changed or mapped
What is Array reduce useful for?
executes a function that takes an array and condenses it down into a single value. Kind of liking folding a piece of paper down to a tiny square
What is the JavaScript Event Loop?
The event loop is a check if the call stack is blocked, if not, it moves the next task in the task queue onto the call stack
What is different between “blocking” and “non-blocking” with respect to how code is executed?
Blocking is synchronous and non blocking is asynchronous.
What is JSON?
JavaScript Object Notation - common data interchange format used to send and store information in computer systems
What are serialization and deserialization?
Serialization is the changing of an object in memory into a string of bytes. Deserialization is the opposite
Why are serialization and deserialization useful?
Many languages use JSON, so it is important so that data from one language can be read by another language. It’s also useful for storing data
How do you serialize data into a JSON string using JavaScript?
the JSON stringify method
How do you deserialize a JSON string using JavaScript?
the JSON parse method