JavaScript Flashcards
Name the primitive types of javascript
- number 2. string 3. boolean 4. null 5. undefined
Describe lexical scoping
Lexical scoping, aka static scoping, refers to the process of determining a variable’s SCOPE based on its position within the body of code. For example, variables declared outside of a function have global scope, whereas variables declared inside a function have local scope (visible to members of the function only)
What is a lambda language
In simple terms, a lambda language is one that allows functions to be passed to other functions, with the passed function being treated like any other variable
In JavaScript, 1 and 1.0 are the same value because _
1 and 1.0 are the same value because JavaScript has no separate integer type, it has a single number type
Describe NaN
NaN is a number value that is the result of an operation that cannot produce a normal result. e.g. multiplying a number with a string
How can detect NaN
You can detect NaN by using the isNaN function, e.g. isNan( number)
In JavaScript, all characters are _ bit wide
16
In JavaScript, all numbers are _ bit floating-point
64
Strings are immutable. Describe
A string cannot be changed once its made. It is, however, easy to create a new string by concatenating other strings together using the + operator
What is a statement
A statement is a sentence or command, which ends with a semicolon. Statements make something happen, e.g. var name; is a statement that declares a variable called name
True or false, blocks in JavaScript do not create a new scope
True, blocks in JavaScript do not create a new scope, so variables should be defined at the top of a function, not in blocks
What are the falsy values?
- false 2. null 3. undefined 4. empty string ‘’ 5. number 0 6. NaN
Explain the switch statement
The switch statement performs a multiway branch by comparing the expression against cases for equality switch (expression){ case expression1 : (case can contain one or more expressions) //code to be executed break; case expression2: //code to be executed break; default: //code to be executed }
What is an expression
An expression is a phrase that can be evaluated into a value by the JavaScript interpreter. For example var name = “Succeed”
What is the global object
Its a common namespace where all top-level variables for all compilation units are stored
What are the advantages of JavaScript having a single number type?
- Problems of overflow in short integers are avoided 2. A large class of numeric type errors is avoided
When is it appropriate to use a switch statement?
It is appropriate to use a switch statement when you are testing an expression is based on a single integer, string, etc…as opposed to if-then-else which can be used with an expression based on ranges of values or conditions
What does the throw statement do?
The throw statement raises an exception. If it is part of a try/catch block, execution is passed to the catch clause
Describe an expression statement
An expression statement can: 1. assign values to one or more variables or members 2. invoke a method 3. delete an object property
Define a JavaScript object
A JavaScript object is a mutable container of properties, where each property has a name and a value pair
Demonstrate an object literal
var person = {
name : 'John', age : 30
};
JavaScript includes a _ feature that allows an object to _ the properties of _
JavaScript includes a prototype linkage feature that allows an object to inherit the properties of another object
When is it appropriate to use quotes around the name of an object property
It is appropriate to use quotes around the name of an object property when the name has a space eg
var person = {
“first name” : ‘John’
}
var animal = {
type : ‘dog’,
age : 2
}
console.log(animal.breed.breedType)
What is the outcome?
TypeError because there is no defined breed.breedType
Demonstrate how objects are passed around by reference
var person = {
name : ‘Jane’,
age : 30 ,
gender : ‘female’
};
var jim = person;
jim. gender = ‘male’;
console. log(person.gender)
Outcome is male because person and jim reference the same object
True or false, every object is linked to a prototype object from which it can inherit properties.
True, every object is linked to a prototype object from which it can inherit properties. All objects created from object literals are linked to Object.prototype, an object that comes standard with JS
True or false, the protoype link has no effect on updating
True, the prototype link has no effect on updating, i.e. when an update is made on the inheriting object, the prototype object remains unchanged
Describe the prototype chain
The prototype chain is a link used by JavaScript to search for an object’s specific property. If JavaScript does not find the property within the object itself, it goes up the link, looking for the property in the object’s prototype, until it goes all the way to Object.prototype
The prototype relationship is dynamic. Explain
Any changes that are made to the prototype will be immediately visible in all objects that are dependent on the prototype
True or false, the hasOwnProperty method looks at the prototype chain
False, the hasOwnProperty method does not look at the prototype chain, as a result it is useful when looking for properties that are specific to an object
What is the for in statement used for?
The for in statement is used for looping over properties in objects, or elements in array
for ( variable in array | object ) {
statements;
}
Describe one way to minimize the use of global variables
One way to minimize the use of global variables is to create a single global variable for your application
var myApp = {}
myApp.person = {
name : ‘John’
age : 30
};
myApp.animal = {
type : ‘dog’,
breed : ‘ german sheppard’
}
Describe the roles(s) of a function
JavaScript functions do the following:
- form the base of JavaScript’s modular unit
- enclose statements
- encapsulate information
- enable code reuse
- enable code composition
Objects produced from object literals are linked to _
Objects produced from object literals are linked to Object.prototype
Function objects are linked to _ which in turn is linked to _
Function objects are linked to Function.prototype, which in turn is linked to Object.prototype
Demonstrate a function literal
var myFunction = function (a, b) {
return a * b;
};
What is an anonymous function
An anonymous function is a function defined without a name
Invoking a function suspends the _ of the _ function, and passes control to the _ _
Invoking a function suspends the execution of the current function, and passes control to the new function
In additon to the declared parameters, every function receives two additional parameters, _ and _
In addition to the declared parameters, every function receives two additional parameters, this and arguments.
What are the four patterns of invocation in JavaScript
- Method invocation
- Function invocation
- Constructor invocation
- Apply invocation
The this parameter’s value is determined by _ _
The this parameter’s value is determined by the invocation pattern
True or false, there is no runtime error when the number of arguments passed do not match the number of parameters defined for a function
True, there is no runtime error when there is a mismatch between the number of arguments passed and the number of parameters defined for a function
What happens when there are more arguments passed than are defined parameters for a function
The extra arguments are ignored if there are more arguments than defined function parameters
What happens when there are fewer arguments passed to a function than there are parameters defined
The missing arguments will be replaced by undefined