JavaScript Flashcards

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
Q

Demonstrate how objects are passed around by reference

A

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

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

True or false, every object is linked to a prototype object from which it can inherit properties.

A

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

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

True or false, the protoype link has no effect on updating

A

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

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

Describe the prototype chain

A

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

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

The prototype relationship is dynamic. Explain

A

Any changes that are made to the prototype will be immediately visible in all objects that are dependent on the prototype

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

True or false, the hasOwnProperty method looks at the prototype chain

A

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

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

What is the for in statement used for?

A

The for in statement is used for looping over properties in objects, or elements in array

for ( variable in array | object ) {

 statements;

}

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

Describe one way to minimize the use of global variables

A

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’

}

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

Describe the roles(s) of a function

A

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

Objects produced from object literals are linked to _

A

Objects produced from object literals are linked to Object.prototype

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

Function objects are linked to _ which in turn is linked to _

A

Function objects are linked to Function.prototype, which in turn is linked to Object.prototype

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

Demonstrate a function literal

A

var myFunction = function (a, b) {

return a * b;

};

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

What is an anonymous function

A

An anonymous function is a function defined without a name

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

Invoking a function suspends the _ of the _ function, and passes control to the _ _

A

Invoking a function suspends the execution of the current function, and passes control to the new function

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

In additon to the declared parameters, every function receives two additional parameters, _ and _

A

In addition to the declared parameters, every function receives two additional parameters, this and arguments.

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

What are the four patterns of invocation in JavaScript

A
  1. Method invocation
  2. Function invocation
  3. Constructor invocation
  4. Apply invocation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

The this parameter’s value is determined by _ _

A

The this parameter’s value is determined by the invocation pattern

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

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

A

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

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

What happens when there are more arguments passed than are defined parameters for a function

A

The extra arguments are ignored if there are more arguments than defined function parameters

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

What happens when there are fewer arguments passed to a function than there are parameters defined

A

The missing arguments will be replaced by undefined

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

True or false, arguments passed to a function are not checked for type

A

True, there is no type checking on the argument values, any type of value can be passed to any parameter

47
Q

Describe and demonstrate post-increment assignment

A

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
Q

Describe and demonstrate pre-increment

A

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
Q

What is a compound operator?

A

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
Q

How do you find out the type of a value or variable?

A

The typeof operator can be used to find the type of a value or variable.

51
Q

What data type does typeof return?

A

The typeof operator returns a string that represents the data type e.g.

“string”, “number”, “boolean”, “function”, “undefined”, “object”

52
Q

What is Infinity

A

Infinity is a special JavaScript value that is too big for JavaScript to handle

53
Q

What is the result of Infinity - Infinity

A

Infinity - Infinity = NaN

54
Q

var s = ‘1’;

s++;

console.log( s + typeof s);

What is the outcome?

A

var s = ‘1’;

s++;

console.log( s + typeof s); // 2 number

55
Q

var b = !!false;

console.log(b);

what is the outcome

A

var b = !!false;

console.log(b); // false

if you use double negation you will get the original value

56
Q

what are the results of the following

true && true

true && false

false && true

false && false

true || true

true || false

false || true

false || false

A

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
Q

What is the outcome?

true || (b = 6);

A

true || (b = 6); // true

58
Q

What is the outcome?

true && ( b = 6);

A

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
Q

What is the outcome?

var mynumber = mynumber || 10;

A

mynumber = mynumber || 10 ; // 10, because mynumber was not

previously initialized

60
Q

What is the outcome?

var mynumber = 0;

mynumber = mynumber || 10;

A

var mynumber = 0;

mynumber = mynumber || 10; // 10

the outcome is 10 because JavaScript overwrites the previously initialized value

61
Q

Why is it recommended to use the === equality operator

A

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
Q

What is the outcome?

console.log(myvar);

A

console.log(myvar); // ReferenceError : myvar is not defined

63
Q

What is the outcome?

console.log(typeof myvar);

A

console.log(typeof myvar); // “undefined”

typeof always returns a string name of the data type, in this case the data type is defined

64
Q

What is the outcome?

var myvar;

console.log(myvar);

A

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
Q

var myvar = null;

console.log(typeof myvar);

A

var myvar = null;

console.log(typeof myvar); // “object”

66
Q

What is the outcome?

console.log(var i = 3 + undefined);

A

console.log(var i = 3 + undefined); // NaN

This is because undefined is not converted to a number

67
Q

What is the outcome?

console.log(var i = 1 + null);

A

console.log(var i = 1 + null); // 1

This is because null is converted to 0

68
Q

What happens if you add a new element to an array, but leave a
gap in the array?

A

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
Q

How do you delete an element in an array?

A

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
Q

Illustrate a good way to check if a variable is defined

A

var myVariable = “”;

if(typeof myVariable !== undefined) {

console.log(“variable exists”);

} else {

console.log(“variable does not exist”);

}

71
Q

Rewrite the following using the ternary operator

var myName = ‘Saki’;

var result = ‘’;

if(myName === ‘Saki’) {

result = "true";

} else {

result = “false”;

}

A

var myName = ‘Saki’;

var result = myName === ‘Saki’ ? ‘true’ : ‘false’;

72
Q

function sum(a,b) {

return a + b;

}

console.log(sum(2));

What is the outcome

A

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
Q

In functions, what is arguments?

A

Arguments is a bonus parameter that consists of an array-like object containing the arguments passed to the function

74
Q

Demonstrate how the arguments parameter works

A

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
Q

What is the outcome?

console. log(parseInt(‘34’));
console. log(parseInt(‘4K’));
console. log(parseInt(‘K55’));

A

console. log(parseInt(‘34’)); // 34
console. log(parseInt(‘4K’)); // 4
console. log(parseInt(‘K55’)); // NaN

76
Q

What is the function isFinite() used for?

A

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
Q

What is the function encodeURI( ) used for?

A

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
Q

What is the outcome?

var a = 50;

function myFunction( ) {

console.log(a);

var a = 2;

console.log(a);

}

myFunction( );

A

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
Q

Illustrate function literal notation

A

function literal notation :

   var person = function( ) {

      return name;

   }
80
Q

Illustrate a function expression

A

function expression :

function ( ) {

 return name;

}

named function expression:

function **myFunction** ( ) {

 return name;

}

81
Q

What is a callback function?

A

A callback function is a function that is passed as a parameter to another function, and then called inside that other function

82
Q

Illustrate the use of a callback function

A

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
Q

When are callback functions most useful in javaScript?

A

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
Q

Demonstrate the use of an anonymous callback function

A

function getInputValues(a, b, callback) {

 var num1 =

}

85
Q

Demonstrate a self invoking function

A

(function(name) {

console.log(‘Hello ‘ + name); // Hello Jane

return;

}());

86
Q

Describe one good use of self-invoking functions

A

Self invoking functions are useful when you want to avoid creating extra global variables

( function () {

 // code

} ( ) ) ;

87
Q

Demonstrate the use of a private function

A

function outer (param) {

function inner( inputvalue) { // private function

 return inputvalue \* 5;

}

return inner( param );

}

88
Q

What are the advantages of using private functions?

A
  1. You keep the global namespace clean
  2. You can control access to your functions
89
Q

Illustrate a function that returns another function, and invoke the returned function immediately

A

function first ( ) {

console.log(‘first’);

return function returned ( ) {

console.log( 'returned' );

};

}

first( ) ( ); // second set of parenthesis invokes the second function

90
Q

What is a closure?

A

A closure can be defined as a function that keeps a link to its parent scope, even after the parent has returned

91
Q

Why are all functions considered closures?

A

All functions are considered closures because every function maintains access to the global scope, which is never destroyed

92
Q

Demonstrate a closure

A

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
Q

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.

A

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
Q

True or false, defining an array with [] is called array literal notation

A

True, defining an array with [] is called array literal notation

95
Q

Define object literal notation

A

Object literal notation is the definition of an object with curly braces

var myObject = { name : ‘Saki’ } ;

96
Q

What is the [[Put]] method?

A

The [[Put]] method is a method invoked by JavaScript everytime a property is initially added to an object.

97
Q

What does the [[Put]] method do?

A

The [[Put]] method creates a spot for a property in the object in which the property is defined

98
Q

What is the result of calling the [[Put]] method?

A

The result of calling the [[Put]] method is to creation of the own property on the object

99
Q

An _ _ indicates that the specific instance of an object owns that _

A

An own property indicates that the specific instance of an object owns that property

100
Q

When a value is assigned to an existing object property, the _ operation is called

A

When a value is assigned to an existing object property, the [[set]] operation is called

101
Q

When checking if a property exists, why is the following code is unreliable?

if ( person.age ) {

 //do something

}

A

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
Q

The _ operator looks for a property in a specific object and returns _ if it finds it

A

The in operator looks for a property in a specific object and returns true if it finds it

103
Q

What does the hasOwnProperty( ) method do?

A

The hasOwnProperty( ) method checks if a specific property exists on an object, and returns true if it does

104
Q

What is the difference between the in property and the hasOwnProperty method?

A

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
Q

What is the outcome?

var myObject = {

name : ‘Saki’,

age : 36

};

delete myObject.age;

console.log( myObject.age );

A

var myObject = {

name : ‘Saki’,

age : 36

};

delete myObject.age;

console.log( myObject.age ); // undefined

106
Q

How is the for-in loop used to enumate all enumerable object properties?

A

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
Q

Show an example of a data property

A

Example of a data property

var myObject = {

name : ‘Saki’

}

Data properties contain a value, like name in the above example

108
Q

What do accessor properties do?

A

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
Q

Illustrate the syntax to define an accessor property using an object literal

A

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
Q

When are accessor properties most useful

A

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
Q

What is the outcome?

var myObject = {

_name : ‘Saki’,

get name( ) {

 return this.\_name;

}

}

console.log( myObject.name(‘Try’);

A

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
Q

If a variable is expected to hold an object at a later point, it is advisable to initialize it with null, why?

A

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
Q

In JavaScript, what does double parenthesis in a function call do?

A

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( )( )