JavaScript Flashcards
What is the object type?
The object type refers to a compound value where you can set properties (named locations) that each hold their own values of any type
Explain arrays in JavaScript
An array is an object that holds values in numerically indexed positions.
What is typeof operator?
JavaScript provides a typeof operator that can examine a value and tell you what type it is:
Explain equality in JavaScript
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
What is Scope in JavaScript?
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.
Explain Values and Types in JavaScript
JavaScript has typed values, not typed variables.
- string
- number
- boolean
- null and undefined
- object
- symbol
What is let keyword in JavaScript?
- creates declarations for variables at the function level,
- lets you declare variables to belong to individual blocks (pairs of { .. }), using the let keyword.
Explain the same-origin policy with regards to JavaScript.
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.
Is there anyway to force using strict mode in Node.js?
“use strict”
or
node –use_strict to apply to whole app
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?
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.
What is strict mode?
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.
What’s the difference between Host objects and Native objects?
- 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.
What language constructions do you use for iterating over object properties?
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.
What language constructions do you use for iterating over array items?
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.
What are some of the advantages of writing JavaScript code in a language that compiles to JavaScript?
- 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.