JavaScript Chapter 2
Chapter 2
Theoretical Questions
2.1 Can you name two programming paradigms important for JavaScript app developers?
JavaScript is a multi-paradigm language, supporting imperative/procedural programming along with OOP (Object-Oriented
Programming) and functional programming. JavaScript supports OOP with prototypal inheritance.
2.2 What is functional programming?
Functional programming produces programs by composing mathematical functions and avoids shared state & mutable data.
Lisp (specified in 1958) was among the first languages to support functional programming, and was heavily inspired by lambda
calculus. Functional programming is an essential concept in JavaScript (one of the two pillars of JavaScript). Several common
functional utilities were added to JavaScript in ES5.
Focus is on: - Function purity - Avoiding side-effects - Simple function composition
2.3 What is the difference between classical inheritance and prototypal inheritance?
Class Inheritance: instances inherit from classes (like a blueprint - a description of the class), and create sub-class relationships:
hierarchical class taxonomies. Instances are typically instantiated via constructor functions with the new keyword. Class
inheritance may or may not use the class keyword from ES6.
Prototypal Inheritance: instances inherit directly from other objects. Instances are typically instantiated via factory functions
or Object.create(). Instances may be composed from many different objects, allowing for easy selective inheritance.
2.4 When is prototypal inheritance an appropriate choice?
There is more than one type of prototypal inheritance:
• Delegation (i.e., the prototype chain).
• Concatenative (i.e. mixins, Object.assign()).
• Functional (Not to be confused with functional programming. A function used to create a closure for private state/encapsulation).
Each type of prototypal inheritance has its own set of use-cases, but all of them are equally useful in their ability to enable composition,
which creates has-a or uses-a or can-do relationships as opposed to the is-a relationship created with class inheritance.
2.5 What is asynchronous programming, and why is it important in JavaScript?
Synchronous programming means that, barring conditionals and function calls, code is executed sequentially from top-to-bottom,
blocking on long-running tasks such as network requests and disk I/O.
Asynchronous programming means that the engine runs in an event loop. When a blocking operation is needed, the request is
started, and the code keeps running without blocking for the result. When the response is ready, an interrupt is fired, which causes
an event handler to be run, where the control flow continues. In this way, a single program thread can handle many concurrent
operations.
User interfaces are asynchronous by nature, and spend most of their time waiting for user input to interrupt the event loop and
trigger event handlers. Node is asynchronous by default, meaning that the server works in much the same way, waiting in a loop
for a network request, and accepting more incoming requests while the first one is being handled.
This is important in JavaScript, because it is a very natural fit for user interface code, and very beneficial to performance on the
server.
2.6 What are JavaScript data types?
In JavaScript, there are three primary data types, two composite data types, and two special data types.
Primary Data Types
• String
• Number
• Boolean
Composite Data Types
• Object
• Array
Special Data Types
• Null
• Undefined
2.7 What is the difference between "==" and "==="?
"==" checks only for equality in value whereas "===" is a stricter equality test and returns false if either the value or the type of
the two variables are different. So, the second option needs both the value and the type to be the same for the operands.
2.8 What is an undefined value in JavaScript?
Undefined value means the:
• Variable used in the code doesn’t exist
• Variable is not assigned to any value
• Property doesn’t exist
2.9 What are the different types of errors in JavaScript?
There are three types of errors:
• Load time errors: Errors which come up when loading a web page like improper syntax errors are known as Load time errors
and it generates the errors dynamically.
• Run time errors: Errors that come due to misuse of the command inside the HTML language.
• Logical Errors: These are errors that occur due to the wrong logic performed on a function.
2.10 Define event bubbling
JavaScript allows DOM elements to be nested inside each other. In such a case, if the handler of the child is clicked, the handler
of parent will also work as if it were clicked too.
2.11 What is the significance, and what are the benefits, of including use strict at
the beginning of a JavaScript source file?
use strict is a way to voluntarily enforce stricter parsing and error handling on your JavaScript code at runtime. Code errors
that would otherwise have been ignored or would have failed silently will now generate errors or throw exceptions. In general, it
is a good practice.
Some of the key benefits of strict mode include:
Makes debugging easier. Code errors that would otherwise have been ignored or would have failed silently will now generate
errors or throw exceptions, alerting you sooner to problems in your code and directing you more quickly to their source.
Prevents accidental globals. Without strict mode, assigning a value to an undeclared variable automatically creates a global
variable with that name. This is one of the most common errors in JavaScript. In strict mode, attempting to do so throws an error.
Eliminates this coercion. Without strict mode, a reference to a this value of null or undefined is automatically coerced to the
global. This can cause many headfakes and pull-out-your-hair kind of bugs. In strict mode, referencing a a this value of null or
undefined throws an error.
Disallows duplicate property names or parameter values. Strict mode throws an error when it detects a duplicate named
property in an object (e.g., var object ={foo:"bar", foo:"baz"};) or a duplicate named argument for a function
(e.g., function foo(val1, val2, val1){}), thereby catching what is almost certainly a bug in your code that you
might otherwise have wasted lots of time tracking down.
Makes eval() safer. There are some differences in the way eval() behaves in strict mode and in non-strict mode. Most
significantly, in strict mode, variables and functions declared inside of an eval() statement are not created in the containing
scope (they are created in the containing scope in non-strict mode, which can also be a common source of problems).
Throws error on invalid usage of delete. The delete operator (used to remove properties from objects) cannot be used on
non-configurable properties of the object. Non-strict code will fail silently when an attempt is made to delete a non-configurable
property, whereas strict mode will throw an error in such a case.
2.12 What are Screen objects?
Screen objects are used to read the information from the client’s screen. The properties of screen objects are -
• AvalHeight: Gives the height of client’s screen
• AvailWidth: Gives the width of client’s screen.
• ColorDepth: Gives the bit depth of images on the client’s screen
• Height: Gives the total height of the client’s screen, including the taskbar
• Width: Gives the total width of the client’s screen, including the