JavaScript Flashcards

1
Q

What is the object type?

A

The object type refers to a compound value where you can set properties (named locations) that each hold their own values of any type

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

Explain arrays in JavaScript

A

An array is an object that holds values in numerically indexed positions.

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

What is typeof operator?

A

JavaScript provides a typeof operator that can examine a value and tell you what type it is:

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

Explain equality in JavaScript

A
Strict comparison (e.g., ===) checks for value equality without allowing coercion i.e. also check type
e.g. 24 === '24' //returns false

Abstract comparison (e.g. ==) checks for value equality with coercion allowed i.e. ignores type

e.g. 24==’24’ //returns true

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

What is Scope in JavaScript?

A

Scope is basically a collection of variables as well as the rules for how those variables are accessed by name. Only code inside that function can access that function’s scoped variables.

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

Explain Values and Types in JavaScript

A

JavaScript has typed values, not typed variables.

  • string
  • number
  • boolean
  • null and undefined
  • object
  • symbol
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is let keyword in JavaScript?

A
  • creates declarations for variables at the function level,

- lets you declare variables to belong to individual blocks (pairs of { .. }), using the let keyword.

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

Explain the same-origin policy with regards to JavaScript.

A

The same-origin policy prevents JavaScript from making requests across domain boundaries. An origin is defined as a combination of URI scheme, hostname, and port number. This policy prevents a malicious script on one page from obtaining access to sensitive data on another web page through that page’s Document Object Model.

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

Is there anyway to force using strict mode in Node.js?

A

“use strict”
or
node –use_strict to apply to whole app

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

Why would you use something like the load event? Does this event have disadvantages? Do you know any alternatives, and why would you use those?

A

The load event fires at the end of the document loading process. At this point, all of the objects in the document are in the DOM, and all the images, scripts, links and sub-frames have finished loading.

The DOM event DOMContentLoaded will fire after the DOM for the page has been constructed, but do not wait for other resources to finish loading. This is preferred in certain cases when you do not need the full page to be loaded before initializing.

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

What is strict mode?

A

a that allows you to place a program, or a function, in a “strict” operating context. This strict context prevents certain actions from being taken and throws more exceptions.

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

What’s the difference between Host objects and Native objects?

A
  • Native objects are objects that are part of the JavaScript language defined by the ECMAScript specification, such as String, Math, RegExp, Object, Function, etc.
  • Host objects are provided by the runtime environment (browser or Node), such as window, XMLHTTPRequest, etc.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What language constructions do you use for iterating over object properties?

A

for loops - for (var property in obj) { console.log(property); }. However, this will also iterate through its inherited properties, and you will add an obj.hasOwnProperty(property) check before using it.

Object.keys() - Object.keys(obj).forEach(function (property) { … }). Object.keys() is a static method that will lists all enumerable properties of the object that you pass it.

Object.getOwnPropertyNames() - Object.getOwnPropertyNames(obj).forEach(function (property) { … }). Object.getOwnPropertyNames() is a static method that will lists all enumerable and non-enumerable properties of the object that you pass it.

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

What language constructions do you use for iterating over array items?

A

for loops - for (var i = 0; i < arr.length; i++). The common pitfall here is that var is in the function scope and not the block scope and most of the time you would want block scoped iterator variable. ES2015 introduces let which has block scope and it is recommended to use that instead. So this becomes: for (let i = 0; i < arr.length; i++).

forEach - arr.forEach(function (el, index) { … }). This construct can be more convenient at times because you do not have to use the index if all you need is the array elements. There are also the every and some methods which will allow you to terminate the iteration early.

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

What are some of the advantages of writing JavaScript code in a language that compiles to JavaScript?

A
  • Fixes some of the longstanding problems in JavaScript and discourages JavaScript anti-patterns.
  • Enables you to write shorter code, by providing some syntactic sugar on top of JavaScript, which I think ES5 lacks, but ES2015 is awesome.
  • Static types are awesome (in the case of TypeScript) for large projects that need to be maintained over time.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are some of the disadvantages of writing JavaScript code in a language that compiles to JavaScript?

A
  • Require a build/compile process as browsers only run JavaScript and your code will need to be compiled into JavaScript before being served to browsers.
  • Debugging can be a pain if your source maps do not map nicely to your pre-compiled source.
  • Most developers are not familiar with these languages and will need to learn it. There’s a ramp up cost involved for your team if you use it for your projects.
  • Smaller community (depends on the language), which means resources, tutorials, libraries, and tooling would be harder to find.
  • IDE/editor support might be lacking.
  • These languages will always be behind the latest JavaScript standard.
  • Developers should be cognizant of what their code is being compiled to — because that is what would actually be running, and that is what matters in the end.
17
Q

Explain event bubbling and how one may prevent it

A

Event bubbling is the concept in which an event triggers at the deepest possible element, and triggers on parent elements in nesting order. As a result, when clicking on a child element one may exhibit the handler of the parent activating.

One way to prevent event bubbling is using event.stopPropagation() or event.cancelBubble on IE < 9.

18
Q

What does use strict do?

A

The use strict literal is entered at the top of a JavaScript program or at the top of a function and it helps you write safer JavaScript code by throwing an error if a global variable is created by mistake.

19
Q

Why is it, in general, a good idea to leave the global scope of a website as-is and never touch it?

A

Every script has access to the global scope, and if everyone uses the global namespace to define their variables, collisions will likely occur. Use the module pattern (IIFEs) to encapsulate your variables within a local namespace.

20
Q

What is a Polyfill?

A

A polyfill is essentially the specific code (or plugin) that would allow you to have some specific functionality that you expect in current or “modern” browsers to also work in other browsers that do not have the support for that functionality built in.

Polyfills are not part of the HTML5 standard
Polyfilling is not limited to Javascript

21
Q

What’s the difference between throw Error(‘msg’) vs throw new Error(‘msg’)?

A

Both are fine; the function call Error(…) is equivalent to the object creation expression new Error(…) with the same arguments.