JavaScript Flashcards

(113 cards)

1
Q

Name the primitive types of javascript

A
  1. number 2. string 3. boolean 4. null 5. undefined
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Describe lexical scoping

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a lambda language

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

In JavaScript, 1 and 1.0 are the same value because _

A

1 and 1.0 are the same value because JavaScript has no separate integer type, it has a single number type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Describe NaN

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can detect NaN

A

You can detect NaN by using the isNaN function, e.g. isNan( number)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

In JavaScript, all characters are _ bit wide

A

16

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

In JavaScript, all numbers are _ bit floating-point

A

64

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Strings are immutable. Describe

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a statement

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

True or false, blocks in JavaScript do not create a new scope

A

True, blocks in JavaScript do not create a new scope, so variables should be defined at the top of a function, not in blocks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the falsy values?

A
  1. false 2. null 3. undefined 4. empty string ‘’ 5. number 0 6. NaN
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Explain the switch statement

A

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 }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is an expression

A

An expression is a phrase that can be evaluated into a value by the JavaScript interpreter. For example var name = “Succeed”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the global object

A

Its a common namespace where all top-level variables for all compilation units are stored

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the advantages of JavaScript having a single number type?

A
  1. Problems of overflow in short integers are avoided 2. A large class of numeric type errors is avoided
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

When is it appropriate to use a switch statement?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What does the throw statement do?

A

The throw statement raises an exception. If it is part of a try/catch block, execution is passed to the catch clause

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Describe an expression statement

A

An expression statement can: 1. assign values to one or more variables or members 2. invoke a method 3. delete an object property

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Define a JavaScript object

A

A JavaScript object is a mutable container of properties, where each property has a name and a value pair

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Demonstrate an object literal

A

var person = {

name :  'John',

age : 30

};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

JavaScript includes a _ feature that allows an object to _ the properties of _

A

JavaScript includes a prototype linkage feature that allows an object to inherit the properties of another object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

When is it appropriate to use quotes around the name of an object property

A

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’

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

var animal = {

type : ‘dog’,

age : 2

}

console.log(animal.breed.breedType)

What is the outcome?

A

TypeError because there is no defined breed.breedType

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
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
26
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
27
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
28
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
29
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
30
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
31
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; }
32
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' }
33
Describe the roles(s) of a function
JavaScript functions do the following: 1. form the base of JavaScript's modular unit 2. enclose statements 3. encapsulate information 4. enable code reuse 5. enable code composition
34
Objects produced from object literals are linked to \_
Objects produced from object literals are linked to **Object.prototype**
35
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
36
Demonstrate a function literal
var myFunction = function (a, b) { return a \* b; };
37
What is an anonymous function
An anonymous function is a function defined without a name
38
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**
39
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**.
40
What are the four patterns of invocation in JavaScript
1. Method invocation 2. Function invocation 3. Constructor invocation 4. Apply invocation
41
The *this* parameter's value is determined by _ \_
The *this* parameter's value is determined by the **invocation pattern**
42
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
43
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
44
What happens when there are fewer arguments passed to a function than there are parameters defined
The missing arguments will be replaced by **undefined**
45
46
True or false, arguments passed to a function are not checked for type
True, there is no type checking on the argument values, any type of value can be passed to any parameter
47
Describe and demonstrate post-increment assignment
Post-increment is when the input value is assigned after it is returned var a = 120; var b = a++; console.log(a, b) // a = 121, b = 120
48
Describe and demonstrate pre-increment
With pre-increment, the value is incremented and then returned var a = 123; var b = ++a; console.log(a, b); // a = 124, b = 124
49
What is a compound operator?
A compound operator is a combination of an operator and an assignment var a = 10; a += 5; // same as a = a + 5 console.log(a) // 15;
50
How do you find out the type of a value or variable?
The typeof operator can be used to find the type of a value or variable.
51
What data type does typeof return?
The typeof operator returns a string that represents the data type e.g. "string", "number", "boolean", "function", "undefined", "object"
52
What is Infinity
Infinity is a special JavaScript value that is too big for JavaScript to handle
53
What is the result of Infinity - Infinity
Infinity - Infinity = NaN
54
var s = '1'; s++; console.log( s + typeof s); What is the outcome?
var s = '1'; s++; console.log( s + typeof s); // 2 number
55
var b = !!false; console.log(b); what is the outcome
var b = !!false; console.log(b); // false if you use double negation you will get the original value
56
what are the results of the following true && true true && false false && true false && false true || true true || false false || true false || false
true && true - true true && false - false false && true - false false && false - false true || true - true true || false - true false || true - true false || false - false
57
What is the outcome? true || (b = 6);
true || (b = 6); // true
58
What is the outcome? true && ( b = 6);
true && (b = 6); // 6 When JavaScript encounters a non-Boolean expression as an operand in a logical operation, the non-Boolean expression is returned as a result
59
What is the outcome? var mynumber = mynumber || 10;
mynumber = mynumber || 10 ; // 10, because mynumber was not previously initialized
60
What is the outcome? var mynumber = 0; mynumber = mynumber || 10;
var mynumber = 0; mynumber = mynumber || 10; // 10 the outcome is 10 because JavaScript overwrites the previously initialized value
61
Why is it recommended to use the === equality operator
It is recommended to use the === equality operator, because, unlike the == operator, JavaScript does not convert the data types to see if there is a match. With ===, there is only a match if both operands are equal and of the same type
62
What is the outcome? console.log(myvar);
console.log(myvar); // ReferenceError : myvar is not defined
63
What is the outcome? console.log(typeof myvar);
console.log(typeof myvar); // "undefined" typeof always returns a string name of the data type, in this case the data type is defined
64
What is the outcome? var myvar; console.log(myvar);
var myvar; console.log(myvar); // undefined This is because, everytime you declare a variable without initializing it, JavaScript automatically initializes it with the value undefined
65
var myvar = null; console.log(typeof myvar);
var myvar = null; console.log(typeof myvar); // "object"
66
What is the outcome? console.log(var i = 3 + undefined);
console.log(var i = 3 + undefined); // NaN This is because undefined is not converted to a number
67
What is the outcome? console.log(var i = 1 + null);
console.log(var i = 1 + null); // 1 This is because null is converted to 0
68
What happens if you add a new element to an array, but leave a gap in the array?
if you add a new element to an array, but leave a gap in the array, those elements in between that dont exist, return **undefined** if they are accessed.
69
How do you delete an element in an array?
You delete an element in an array by using the **delete** operator var myArray = [1,2,3]; delete myArray[0]; console.log(myArray);// [undefined, 2, 3];
70
Illustrate a good way to check if a variable is defined
var myVariable = ""; if(typeof myVariable !== undefined) { console.log("variable exists"); } else { console.log("variable does not exist"); }
71
Rewrite the following using the ternary operator var myName = 'Saki'; var result = ''; if(myName === 'Saki') { result = "true"; } else { result = "false"; }
var myName = 'Saki'; var result = myName === 'Saki' ? 'true' : 'false';
72
function sum(a,b) { return a + b; } console.log(sum(2)); What is the outcome
function sum(a,b) { return a + b; } console.log(sum(2)); // NaN The outcome is NaN because you are trying to add a number, 2, to undefined
73
In functions, what is arguments?
Arguments is a bonus **parameter** that consists of an **array**-**like** **object** containing the **arguments** passed to the function
74
Demonstrate how the arguments parameter works
function sum() { var i; var result = 0; var no\_of\_parameters = **arguments**.length; for( i = 0; i \< no\_of\_parameters; i++ ) { result = result + **arguments**[i]; } } console.log(sum(1,2,3)) ; // result is 6. arguments is an array-like object containing the arguments passed to the parameter
75
What is the outcome? console. log(parseInt('34')); console. log(parseInt('4K')); console. log(parseInt('K55'));
console. log(parseInt('34')); // 34 console. log(parseInt('4K')); // 4 console. log(parseInt('K55')); // NaN
76
What is the function isFinite() used for?
The function isFinite() is used to check whether a number is not an **infinite** number or **NaN** e.g. isFinite(Infinity) // false isFinite(44); // true
77
What is the function encodeURI( ) used for?
The function encodeURI( ) is used to return a usable url var url = '**my test.asp?name=ståle&car=saab**'; console. log(encodeURI(url)); * *my%20test.asp?name=st%C3%A5le&car=saab**
78
What is the outcome? var a = 50; function myFunction( ) { console.log(a); var a = 2; console.log(a); } myFunction( );
What is the outcome? var a = 50; function myFunction( ) { console.log(a); // undefined - see explanation below var a = 2; console.log(a); // 2 } myFunction( ); a is undefined because of JavaScript's **hoisting** behavior. When a new function is executed, JavaScript moves all declared variables to the **top** of the function, regardless of where there are declared. The important thing to note that is, it is **only** the **declaration** that is **hoisted** to the top, not the assigned values.
79
Illustrate function literal notation
function literal notation : var person = function( ) { return name; }
80
Illustrate a function expression
function expression : function ( ) { return name; } **named** function expression: function **myFunction** ( ) { return name; }
81
What is a callback function?
A callback function is a function that is passed as a parameter to another function, and then called inside that other function
82
Illustrate the use of a callback function
function getInput(a, b, callback) { var number1 = a, number2 = b; callback(number1, number2); } //callback function function processInput(num1, num2) { var result = num1 \* num2; console.log(result); return result; } getInput(4, 5, processInput); // 20
83
When are callback functions most useful in javaScript?
For asynchronous execution, such as making HTTP requests. No need to wait for a response, you can implement a callback function to get the response, while continuing current function execution
84
Demonstrate the use of an anonymous callback function
function getInputValues(a, b, callback) { var num1 = }
85
Demonstrate a self invoking function
(function(name) { console.log('Hello ' + name); // Hello Jane return; }());
86
Describe one good use of self-invoking functions
Self invoking functions are useful when you want to avoid creating extra global variables **(** function () { // code } ( ) **)** ;
87
Demonstrate the use of a private function
function outer (param) { function inner( inputvalue) { // private function return inputvalue \* 5; } return inner( param ); }
88
What are the advantages of using private functions?
1. You keep the global namespace clean 2. You can control access to your functions
89
Illustrate a function that returns another function, and invoke the returned function immediately
function first ( ) { console.log('first'); return function returned ( ) { console.log( 'returned' ); }; } first( ) **( )**; // second set of parenthesis invokes the second function
90
What is a closure?
A closure can be defined as a function that **keeps** a **link** to its **parent scope**, even after the parent has **returned**
91
Why are all functions considered closures?
All functions are considered closures because every function maintains access to the global scope, which is never destroyed
92
Demonstrate a closure
function myName( firstName ) { var message = "hello..." ; function myFullName ( lastName ) { // inner function has access to outer function parameter return message + firstName + " " + lastName; } return myFullName; // return myFullName function } var name = myName( "Saki" ); // at this point, myName function has returned name( "Jabs" ); // logs Hello...Saki Jabs. Even after outer function has returned, inner function still has access to the outer function variable and parameter
93
In a function closure, the function maintains a reference to the scope where it was defined, not to the variables and their values found in the scope during the function definition, demonstrate.
function myNumbers ( ) { var num = 100; return { getNum : function ( ) { return num; } setNum : function (newNum) { num = newNum; } } } var callMyNumbers = myNumbers( ); callMyNumbers.getNum( ) ; // logs 100 callMyNumbers.setNum(200); callMyNumbers.getNum( ) ; // logs 200
94
True or false, defining an array with [] is called array literal notation
True, defining an array with [] is called array literal notation
95
Define object literal notation
Object literal notation is the definition of an object with curly braces var myObject = { name : 'Saki' } ;
96
What is the [[Put]] method?
The [[Put]] method is a method invoked by JavaScript everytime a property is initially added to an object.
97
What does the [[Put]] method do?
The [[Put]] method creates a spot for a property in the object in which the property is defined
98
What is the result of calling the [[Put]] method?
The result of calling the [[Put]] method is to creation of the **own property** on the object
99
An _ \_ indicates that the specific instance of an object owns that \_
An **own property** indicates that the specific instance of an object owns that **property**
100
When a value is assigned to an existing object property, the _ operation is called
When a value is assigned to an existing object property, the **[[set]]** operation is called
101
When checking if a property exists, why is the following code is unreliable? if ( person.age ) { //do something }
The code below is unreliable because of JavaScript's coercion effects. If the value is truthy, then it is true, however, if the value is falsey, then the result is false, even though the property exists. For example, if age is 0, then the result will be false, but the age property exists. if ( person.age ) { //do something }
102
The _ operator looks for a property in a specific object and returns _ if it finds it
The **in** operator looks for a property in a specific object and returns **true** if it finds it
103
What does the hasOwnProperty( ) method do?
The hasOwnProperty( ) method checks if a specific property exists on an object, and returns true if it does
104
What is the difference between the in property and the hasOwnProperty method?
The difference between the **in** property and the **hasOwnProperty** method is that in property searches through the prototype chain for a property, and the hasOwnProperty method looks for only properties that belong to a specific object var myObject = { name : 'Saki', age : 36 }; console. log('toString' in myObject); // true console. log(myObject.hasOwnProperty('toString')); false
105
What is the outcome? var myObject = { name : 'Saki', age : 36 }; delete myObject.age; console.log( myObject.age );
var myObject = { name : 'Saki', age : 36 }; delete myObject.age; console.log( myObject.age ); // **undefined**
106
How is the for-in loop used to enumate all enumerable object properties?
The for-in loop is used to enumate all enumerable object properties by assigning the property name to a variable var property; for( property in myObject ) { console. log( "Name: " + property ); console. log( "Value: " + myObject[property] ); }
107
Show an example of a *data property*
Example of a data property var myObject = { **name** : 'Saki' } Data properties contain a value, like **name** in the above example
108
What do accessor properties do?
Accessor properties define a function to call when the property is called (getter) and a function to call when the property is written ( setter )
109
Illustrate the syntax to define an accessor property using an object literal
var myObject = { \_name : 'Saki', get name( ) { return this.\_name; }, set name(value ) { this.\_name = value; } } ; console.log( myObject.name ); // Saki myObject.name = 'Try'; console.log( myObject.name ); // Try
110
When are accessor properties most useful
Accessor properties are most usefule when 1. you want a special behavior to occur when setting a property value initiates some special behavior 2. reading a value requires the calculation of the desired returned value
111
What is the outcome? var myObject = { \_name : 'Saki', get name( ) { return this.\_name; } } console.log( myObject.name('Try');
var myObject = { \_name : 'Saki', get name( ) { return this.\_name; } } console.log( myObject.name('Try'); In this instance, the \_name property becomes read-only, and attempting to write to it will cause the following: 1. silently fail in nonstrict mode 2. throw an error in strict mode
112
If a variable is expected to hold an object at a later point, it is advisable to initialize it with null, why?
If a variable is expected to hold an object at a later point, it is advisable to initialize it with null because: 1. it is easy to check if the variable is null or not, e.g. if (myVar != null) { ... } 2. a null value is considered to be an empty object pointer
113
In JavaScript, what does double parenthesis in a function call do?
Double parenthesis are used when calling a function that was returned in another function. Example function foo( ) { var myname = 'SJ'; return function( ) { console.log(myname) } } To call the returned anonymous function: foo( )( )