Keyword

Result: 65 questions

What does the following code print to the console?

function blabbermouth() { };
console.log(blabbermouth.name);

 

Answer:

"blabbermouth"

Functions have a name property that returns the name of a function. JavaScript functions are objects and can have properties like any other object.

View

What does the following function print to the console?

function a(x, y, z) {
  return z;
}
console.log(a("blah"));

Answer:

undefined

When too few arguments are supplied to a function, JavaScript assigns the missing arguments to undefined.

View

What does the following code print to the console?

function c(f) {
  return f();
}
function blub() {
  return "monsters";
}
c(blub);

Answer:

"monsters"

The c() function takes a callback function as a parameter. blub is passed to c() as an argument and is invoked within the body of c().

Functions are "first class" in JavaScript because they can be passed as parameters and stored in data structures.

View

What does the following code print to the console?

result = function surfer() {
  return this === window;
}();
console.log(result);

Answer:

true

There are four different ways to invoke functions in JavaScript and this example demonstrates invocation as a function. The keyword "this" equals window for functions that are invoked as functions.

View

The following code illustrates how to define a constructor function and use the constructor function to create an object.

function Mammal() {
  this.warm_blooded = true;
  this.scaly = false;
}
var fido = new Mammal();

Print the scaly property of the fido object to the console.

Answer:

console.log(fido.scaly);

Constructor functions are specialized JavaScript functions for creating objects. Notice that the fido object could also be created with object literal notation.

View

Use object literal notation to create an object assigned to the spot variable, similar to the following constructor function.

function Mammal() {
  this.warm_blooded = true;
  this.scaly = false;
}
var spot = new Mammal();

Answer:

var spot = {
  warm_blooded: true,
  scaly: false  
}

Constructor functions and object literal notation can both be used to create JavaScript objects. Constructor functions are better when multiple similar objects are created, but both object construction techniques can be used to make JavaScript objects.

View

What does the following code print to the console?

function Hat() {
  var brand = "shhh can't tell";
}
var my_hat = new Hat();
console.log(my_hat.brand);

Answer:

undefined

brand is a private variable in the Hat() constructor function, so it can only be accessed by functions within the constructor function. Remember that JavaScript functions have function scope meaning that variables defined in the function are only available within the function itself.

View

The prototype property of constructor functions can also be set to add methods to objects created by constructor functions. JavaScript functions are objects and the prototype property can be used to add methods to the objects that are created by constructor functions.

function Dog(name) {
  this.name = name;
}

Dog.prototype = {
  constructor: Dog,
  bark: function () { return "ruff ruff" }
}

var rover = new Dog("red rover");
console.log(rover.name);  // returns "red rover"
What does the following line print to the console?
console.log(rover.bark());

Answer:

"ruff ruff"

The prototype design pattern is used to JavaScript to give methods to objects created by constructor functions.

View

Type the "this" keyword into the JavaScript web console of your favorite browser (hopefully Chrome or Firefox) and explain what is returned.

Answer:

this  // the window object is returned

The global object is created when the JavaScript interpreter starts and it creates global properties (i.e. NaN), global functions (i.e. parseInt()), built-in constructor functions (Array()), and global objects (Math). The global object for client-side JavaScript is called window.

View

What does the following line return when typed into the JavaScript web console of an internet browser?

this === window

Answer:

true

window is a keyword that refers to the global object. this refers to different objects in different contexts, but it refers to the window object in the top level namespace.

View

Invoke the zombies function.

function zombies() {
  return "We like to eat people";
}

Answer:

zombies();

Invoking a function is just a fancy way to say "running a function" or "executing a function". JavaScript functions are invoked by adding () to the end of the function name.

View

What does the following example print to the console?

console.log((function (x, y) { return x + y })(3, 4));

Answer:

7

The anonymous function in this example is defined and invoked on the same line. JavaScript functions are frequently invoked immediately after they are defined, so be prepared to see this technique used frequently.

View

What does the following example print to the console?

var globalObject = window;
function nestedExample() {
  function innerFunction () {
    return this === globalObject;
  }
  return innerFunction();
}
console.log(nestedExample());

Answer:

true

Due to a design flaw in the language, inner functions use the function invocation context, not the method invocation context, so the value of "this" in the innerFunction() equals the globalObject. Additional quirks and complicated features of the JavaScript language will be covered in subsequent quizzes.

View

Explain what the following code prints to the console.

function pirate () {
  return this;
}
console.log(pirate());

Answer:

The pirate() function returns the global window object. The global window object is created by the JavaScript interpreter when it is started to store built-in constructor functions, global constants, and global properties. When functions are invoked as functions (i.e. they are in the global namespace and are not part of an object or constructor function), the value of the this keyword is the global object.

View

What is Node.JS?

Answer:

Node.js is a web application framework built on Google Chrome's JavaScript Engine(V8 Engine).

Node.js comes with runtime environment on which a Javascript based script can be interpreted and executed (It is analogus to JVM to JAVA byte code). This runtime allows to execute a JavaScript code on any machine outside a browser. Because of this runtime of Node.js, JavaScript is now can be executed on server as well.

Node.js also provides a rich library of various javascript modules which eases the developement of web application using Node.js to great extents.

 

Node.js = Runtime Environment + JavaScript Library

View

What are the benefits of using Node.js?

Answer:

Following are main benefits of using Node.js

  • Aynchronous and Event DrivenAll APIs of Node.js library are aynchronous that is non-blocking. It essentially means a Node.js based server never waits for a API to return data. Server moves to next API after calling it and a notification mechanism of Events of Node.js helps server to get response from the previous API call.

  • Very Fast Being built on Google Chrome's V8 JavaScript Engine, Node.js library is very fast in code execution.

  • Single Threaded but highly Scalable - Node.js uses a single threaded model with event looping. Event mechanism helps server to respond in a non-bloking ways and makes server highly scalable as opposed to traditional servers which create limited threads to handle requests. Node.js uses a single threaded program and same program can services much larger number of requests than traditional server like Apache HTTP Server.

  • No Buffering - Node.js applications never buffer any data. These applications simply output the data in chunks.

View

What's wrong with the following code snippet?

function checkApiKey (apiKeyFromDb, apiKeyReceived) {  
  if (apiKeyFromDb === apiKeyReceived) {
    return true
  }
  return false
}

Answer:

When you compare security credentials it is crucial that you don't leak any information, so you have to make sure that you compare them in fixed time. If you fail to do so, your application will be vulnerable to timing attacks.

But why does it work like that?

V8, the JavaScript engine used by Node.js, tries to optimize the code you run from a performance point of view. It starts comparing the strings character by character, and once a mismatch is found, it stops the comparison operation. So the longer the attacker has right from the password, the more time it takes.

To solve this issue, you can use the npm module called cryptiles.

function checkApiKey (apiKeyFromDb, apiKeyReceived) {  
  return cryptiles.fixedTimeComparison(apiKeyFromDb, apiKeyReceived)
}
View

What is controller in AngularJS

Answer:

In Angular, a Controller is defined by a JavaScript constructor function that is used to augment the Angular Scope.

When a Controller is attached to the DOM via the ng-controller directive, Angular will instantiate a new Controller object, using the specified Controller's constructor function. A new child scope will be created and made available as an injectable parameter to the Controller's constructor function as $scope.

If the controller has been attached using the controller as syntax then the controller instance will be assigned to a property on the new scope.

View

Being JavaScript only framework, application written in AngularJS are not safe and secure.

Quiz

Simple or complex functionality organized in a single or multiple JavaScript files which can be reused throughout your Node.js application is called ________.

Quiz

© 2017 QuizBucket.org