JavaScript Basics Flashcards

1
Q

What is JS comment syntax?

A

// for inline, /* */ for multiline

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

How do we declare a function as a variable?

A
var addTwoNumbers = function(a, b) {
  return a + b;
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do we declare a function?

A
function addTwoNumbers(a, b) {
  return a + b;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does a variable declared without the ‘var’ keyword become?

A

Global in scope.

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

What are the JS primitives (i.e. only things which aren’t objects)?

A

strings, booleans, numbers, ‘undefined’, and ‘null’

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

What is object literal syntax?

A

var person = {
firstName : ‘Boaz’,
lastName : ‘Sender’
};

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

When using bracket notation with an object to retrieve the value of a key, what must the key be?

A

It must be given as a string (with quotes).

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

What is the syntax to declare an object method?

A
var person = {
  firstName : 'Boaz',
  lastName : 'Sender',
  greet : function(name) {
    log( 'Hi, ' + name );
  }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does ‘this’ refer to?

A

The object that is the context in which the function was called. Note that this is NOT necessarily the context in which the function was defined.

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

What is the JS equivalent of Ruby’s send, and how can it be used to remove ambiguity for ‘this’?

A

‘.call’ can be used to ensure that ‘this’ is in the context of the original object definition:

var person = {
firstName : ‘Boaz’,
lastName : ‘Sender’,
greet : function(greeting, punctuation) {
log( greeting + ‘, ‘ + this.firstName + punctuation );
}
};

var sayIt = person.greet;

sayIt.call( person, ‘Hello’, ‘!!1!!1’ );

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

What is JS for loop syntax?

A

for (var loopVar = initialVal; end_condition; increment/decrement){code block per iteration}

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

What are the JS falsy values?

A

undefined, null, NaN, 0, ‘’ (this last one is an empty string)

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

What will the following code return:

log(‘4’ + 3)

A

JS is a loosely typed language, meaning mathematical operations with non-number values will NOT throw an error. This means the code will return:

‘43’

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

How can we call a method on each element of an array?

A

forEach([1, 2, 3], logIfEven);

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

How can we pass an anonymous function?

A
// **Notice the formatting!**
[1, 2, 3].forEach(function (num) {
  if ((num % 2) == 0) {
    console.log(num);
  }
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Say our cat object has a function as a value for the ‘purr’ key. Can we store this in a variable?

A

Yes, JS can treat functions/methods like objects:

// extract the function from the object
var catPurrFunction = cat['purr'];
// call the function
catPurrFunction();
17
Q

Why is ‘this’ necessary in the following code:

var cat = {
  purr: function () {
    console.log("meow");
  },
  purr_more: function () {
    for (var i = 0; i
A

Without this, the method will look for a variable named ‘purr’, which is undefined.

18
Q

What is the JS constructor syntax, and how can we define ivars and methods in the constructor scope?

A

function Kitten(name, age) {

this. name = name;
this. age = age;

this.meow = function () {
console.log(this.name + ‘ says “meow!”’);
};
}

var kitten = new Kitten(“Earl”, 2);

19
Q

What is the naming convention for constructors?

A

They should be CamelCased.

20
Q

How do we call a constructor?

A

With the (required) ‘new’ keyword:

new ClassName(params);

21
Q

What does ‘prototype’ do, and why is it important?

A

This ensures that all instances that the constructor creates will share the same variable/method instance. This saves a LOT of memory by removing redundancy:

function Kitten(name, age) {
  this.name = name;
  this.age = age;
}

Kitten.prototype.meow = function () {
console.log(this.name + ‘ says “meow!”’);
};

k1 = new Kitten("Earl", 2);
k2 = new Kitten("Houdini", 1);
22
Q

Where are prototype properties stored, and what is the preferred way to access them?

A

In the .__proto__ property, best accessed through:

Object.getPrototypeOf(instanceName);

23
Q

Why do ivars/methods declared on an instance appear to overwrite the __proto__ property of the same name?

A

It does not overwrite it; JS checks first to see if the instance has a matching key, and only checks __proto__ if it does NOT find it as a key of the instance:

// error, purr is not defined!
k1.purr();

Kitten.prototype.purr = function () {
console.log(“Purr on, kitten!”);
};

// all of a sudden it works! Because `Kitten.prototype` now has a
// `purr` property, `k1` can purr via its `k1.\_\_proto\_\_` reference to
// `Kitten.prototype`.
k1.purr();
24
Q

What is the difference between the following two method calls:

‘javascript string’.toUpperCase

‘javascript string’.toUpperCase()

A

Without parentheses we are retrieving the property that belongs to the ‘toUpperCase’ key in the String class, but we are NOT invoking it; we are merely retrieving it, presumable to store it, perhaps as a callback. With parentheses, we retrieve the value in the same manner except we ALSO invoke it:

// retrieving a property that is a function:
'javascript string'.toUpperCase // => function toUpperCase() { [native code] }
// invoking the retrieved function:
'javascript string'.toUpperCase() // => "JAVASCRIPT STRING"
25
Q

Explain scope in JS

A

Scopes are nested. A new, inner scope is created each time a function is called. The function can refer to this new, current scope, or anything from the enclosing scope.

26
Q

What is a closure?

A

A function that uses (captures) a variable from an outer scope (a free variable)

27
Q

How can closures be used to pass arguments to a function?

A
function greetTenTimes(name) {
  _.times(10, function () {
    // captures `name` from outside.
    console.log("Hello, " + name + "!");
  });
}
28
Q

What is JS convention for naming private variables?

A

Use a leading underscore

29
Q

How can a closure be used to create a private state?

A

Closing over (capturing) a variable ensures that it cannot be accessed via a key:

function makeCounter() {
  var count = 1;
  return function () {
    return count++;
  };
}

var counterFn = makeCounter();

console. log(counterFn()); // => 1
console. log(counterFn()); // => 2

30
Q

What does placing “use strict”; at the top of a file do?

A

Throws an error if you forget the ‘var’ keyword (prevents inadvertent creation of global vars).

31
Q

Where are global variables stored?

A

As keys in a special object (‘global’ in Node.js, ‘window’ in browser)

32
Q

How do we require a module?

A

You must store it in a variable:

var http = require("http");
OR, for a non-library file include the path:
var myLib = require("./lib");
33
Q

What must we do to have ‘require’ include a function in a file?

A

Add the following to put the function into a special “module” object which can export:

module.exports.functionName = functionName;