Javascript English Flashcards
What is scope?
Variables declared within function are scoped to the function only
What is a promise?
Is an object that is waiting for an async operation to complete.
Is an object that represent the pending result of an asynchronous operation.
What are data types in javascript?
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 would you compare two objects in JavaScript?
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.
What is a potential pitfall with using typeof bar === “object” to determine if bar is an object? How can this pitfall be avoided?
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
What is the significance of, and reason for, wrapping the entire content of a JavaScript source file in a function block?
To create a private namespace and thereby helps avoid potential name clashes between different JavaScript modules and libraries.
What is the significance, and what are the benefits, of including ‘use strict’ at the beginning of a JavaScript source file?
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.
What is NaN? What is its type? How can you reliably test if a value is equal to NaN?
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.
What will the code below output? Explain your answer.
console. log(0.1 + 0.2);
console. log(0.1 + 0.2 == 0.3);
0.30000000000000004
false
Discuss possible ways to write a function isInteger(x) that determines if x is an integer.
function isInteger(x) { return (x^0) === x; }
or
function isInteger(x) { return Math.round(x) === x; }
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);
})();
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.
Write a simple function (less than 80 characters) that returns a boolean indicating whether or not a string is a palindrome.
function isPalindrome(str) { str = str.replace(/\W/g, '').toLowerCase(); return (str == str.split('').reverse().join('')); }
How to empty an array in JavaScript?
For instance,
var arrayList = [‘a’,’b’,’c’,’d’,’e’,’f’];
There are a couple ways we can use to empty an array.
- arrayList = []
This is recommended if you don’t have references to the original array arrayList anywhere else - arrayList.length = 0;
- arrayList.splice(0, arrayList.length);
What is the difference between the function declarations below?
var foo = function(){ // Some code };
function bar(){ // Some code };
The main difference is the function foo is defined at run-time whereas function bar is defined at parse time.
What are the Dangers of Global Variables and How do You Protect Against It?
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.