Javascript English Flashcards

1
Q

What is scope?

A

Variables declared within function are scoped to the function only

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

What is a promise?

A

Is an object that is waiting for an async operation to complete.
Is an object that represent the pending result of an asynchronous operation.

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

What are data types in javascript?

A

There are 2 Data types, primitive type, has a fixed size in memory. (String, Number, Boolean, Symbol, Null and Undefined).
Reference type, do not have a fixed size in memory. The variable stores a reference to the value (Object, Array, Functions, Date)

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

How would you compare two objects in JavaScript?

A

Primitives like strings and numbers are compared by their value, while objects like arrays, dates, and user defined objects are compared by their reference. This means it compares whether two objects are referring to the same location in memory.
If the number of properties doesn’t match, these two objects are not equal. Secondly, you will check each property whether they have the same value. If all the properties have same value, they are equal.

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

What is a potential pitfall with using typeof bar === “object” to determine if bar is an object? How can this pitfall be avoided?

A

Although typeof bar === “object” is a reliable way of checking if bar is an object, the surprising gotcha in JavaScript is that null is also considered an object!

Answer:
console.log((bar !== null) && (typeof bar === “object”)); // logs false

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

What is the significance of, and reason for, wrapping the entire content of a JavaScript source file in a function block?

A

To create a private namespace and thereby helps avoid potential name clashes between different JavaScript modules and libraries.

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

What is the significance, and what are the benefits, of including ‘use strict’ at the beginning of a JavaScript source file?

A

Is a way to voluntarily enforce stricter parsing and error handling on your JavaScript code at runtime. It is a good practice.

Benefits:

  • Makes debugging easier.
  • Prevents accidental globals.
  • Eliminates this coercion.
  • Disallows duplicate property names or parameter values.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is NaN? What is its type? How can you reliably test if a value is equal to NaN?

A

The NaN property represents a value that is “not a number”. This special value results from an operation that could not be performed either because one of the operands was non-numeric (e.g., “abc” / 4), or because the result of the operation is non-numeric (e.g., an attempt to divide by zero).

For one thing, although NaN means “not a number”, its type is, believe it or not, Number:
console.log(typeof NaN === “number”); // logs “true”

A semi-reliable way to test whether a number is equal to NaN is with the built-in function isNaN()

A better solution would either be to use value !== value, which would only produce true if the value is equal to NaN. Also, ES6 offers a new Number.isNaN() function, which is a different and more reliable than the old global isNaN() function.

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

What will the code below output? Explain your answer.

console. log(0.1 + 0.2);
console. log(0.1 + 0.2 == 0.3);

A

0.30000000000000004

false

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

Discuss possible ways to write a function isInteger(x) that determines if x is an integer.

A

function isInteger(x) { return (x^0) === x; }

or

function isInteger(x) { return Math.round(x) === x; }

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

In what order will the numbers 1-4 be logged to the console when the code below is executed? Why?

(function() {
console.log(1);
setTimeout(function(){console.log(2)}, 1000);
setTimeout(function(){console.log(3)}, 0);
console.log(4);
})();

A

1
4
3
2

1 and 4 are displayed first since they are logged by simple calls to console.log() without any delay

2 is displayed after 3 because 2 is being logged after a delay of 1000 msecs (i.e., 1 second) whereas 3 is being logged after a delay of 0 msecs.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Write a simple function (less than 80 characters) that returns a boolean indicating whether or not a string is a palindrome.

A
function isPalindrome(str) {
    str = str.replace(/\W/g, '').toLowerCase();
    return (str == str.split('').reverse().join(''));
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to empty an array in JavaScript?

For instance,

var arrayList = [‘a’,’b’,’c’,’d’,’e’,’f’];

A

There are a couple ways we can use to empty an array.

  1. arrayList = []
    This is recommended if you don’t have references to the original array arrayList anywhere else
  2. arrayList.length = 0;
  3. arrayList.splice(0, arrayList.length);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the difference between the function declarations below?

var foo = function(){ 
    // Some code
}; 
function bar(){ 
    // Some code
};
A

The main difference is the function foo is defined at run-time whereas function bar is defined at parse time.

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

What are the Dangers of Global Variables and How do You Protect Against It?

A

The danger of global variables is that someone else could create a variable with the same name and overwrite the variable you are using. This is a bad idea in any language.

You prevent this in a number of ways. The most common would be to create one global variable that all of your other variables live in.

The other way you can guard against this is by wrapping all of your code in a self-executing function so that any variables that are declared are declared within that function’s scope.

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

How Do You Iterate Through Members in a JavaScript Object?

A

for(var prop in obj){

// bonus points for hasOwnProperty

if(obj.hasOwnProperty(prop)){

    // do something here

}

}

17
Q

Explain the working of timers in JavaScript? Also elucidate the drawbacks of using the timer, if any?

A

Timers are used to execute a piece of code at a set time or also to repeat the code in a given interval of time. This is done by using the functions setTimeout, setInterval and clearInterval.

The setTimeout(function, delay) function is used to start a timer that calls a particular function after the mentioned delay. The setInterval(function, delay) function is used to repeatedly execute the given function in the mentioned delay and only halts when cancelled. The clearInterval(id) function instructs the timer to stop.

Timers are operated within a single thread, and thus events might queue up, waiting to be executed.

18
Q

What are JavaScript Cookies?

A

Cookies are the small test files stored in a computer and it gets created when the user visits the websites to store information that they need.

Cookies are saved in the server.

19
Q

What are the 3 states of a Promise?

A
  • Unresolved: Waiting for something to finish
  • Resolved: Something finished and it all went ok
  • Rejected: Something finished and something went bad.