JS1 Salesforce Certified JavaScript Developer I
Using loose equality (==) list values equal to NaN
Trick question! Nothing is loosely equal to NaN Not even NaN!!!!!
What is localStorage? How do I do things and stuff with it?
localStorage will *access a Storage object* for the Document's origin and *store across browser sessions*. Unlike sessionStorage (cleared when the page closes) localStorage does not expire.* note: http:// and https:// localStorage are stored separately from each other! how to do things and stuff: *Set a value* localStorage.setItem('name','Bukcy'); *Read a value* const name = localStorage.getItem('name'); //Bukcy *Remove a value* localStorage.removeItem('name'); *Bye bye all local storage cya* localStorage.clear(); For the paranoid, localStorage in incognito mode will be cleared after the last tab is closed.
What is the result of the following: typeof function() {}
"function"
Why does everyone keep saying that functions are "first-class citizens"?
In JavaScript, *functions are actually objects*. They are a special type of Function object. So *you can do object things* with the functions.
What is the result of the following: typeof [1,2,3,4,5]
"object" *NOT* array!
What is typeof null
"object" Don't forget to read about your 6 primitives and you'll probably remember this.
What is typeof NaN?
*"Number"* Surprise!
Object.is() is.... what?
*Determines whether two values are the same value*. Syntax: Object.is(value1, value2); (returns Boolean* If one of the following is true, then Object.is will return true. - both undefined - both null - both true or both false - both strings same length, same chars, same order - same object *reference* - both numbers and -- both +0 -- both-0 -- both NaN -- or both non-zero both not NaN and both have the same value *this is not the same as == or ===* == will apply coercions while .is won't === says -0 === +0 and Number.NaN is not equal to NaN
What are the three states of a Promise?
*Pending*: initial state, neither fulfilled or rejected *Rejected*: operation failed *Fulfilled*: operation completed successfully Here's a mnemonic for you: *FPR: F Promises, Really*
What are the Core Modules in node.js?
*http* required to create a node.js http server *url* used for URL resolution and parsing *querystyring* used for querystring systems *path* contains procedures fore file paths *fs* used for file I/O You can use a core module with require: const http = require('http');
True or False? text.search(/regex/) returns true or false
False. it returns the position of where the term was found, or -1 if not found.
What are private packages?
If you pay npm, *you can use the npm registry to host code only visible to you and certain collaborators*, allowing you to manage and use private code along with public code in projects. Private packages always have a scope, and scoped packages are private by default. When you're using private packages, you're big time.
Define Object.assign() and its parameters
Object.assign() copies all *enumerable own properties* from one or more source objects to a target object. It returns a *reference* to the target object. *Own* properties are non-inherited properties. *Enumerables* are properties available in a loop Example const target = { a: 2, b: 2}; const source1 = { a: 3, c: 5}; const source2 = { c: 1, d: 'blastoffff!'}; const elon = Object.assign(target, source1, source2); console.log(elon); // {a:3,b:2,c:1,d:"blastoffff!"} Note: properties are overwritten in the order of left to right parameters.
Destructuring sounds cool in general. What is it and how can I destructure things?
Yes. Destructuring makes you cool. Once you get the hang of it, you will blow the feeble minds of your fellow developers with your destructuring. A destructuring assignment is a JavaScript expression that *makes it possible to unpack values from arrays or properties from objects* into new variables. *Array destructuring syntax* let name, ship, countdown; [name, ship, ...countdown] = ['Elon','Dragon',3,2,1,'blastoff!']; console.log(name); // Elon console.log(ship); // Dragon console.log(countdown); // [3,2,1,'blastoff!'] You can even do it like this and refactor your code like you're using 8zip: let [name, ship, ...countdown] = ['elon','dragon',3,2,1,'blastoff!']; *Object destructuring syntax* let ship = {color:'red',name:'Dragon'}; let {color,name} = ship; console.log(`The ${name} is ${color}`); // The color is red Destructuring can be done in function arguments too function goToIss([name,,...rest]) { console.log(name,rest); } goToIss(['Dragon','Elon',3,2,1,'blastoff!']); // Dragon [3, 2, 1, "blastoff!"]
tell me EXACTLY what slice does for example ['a','b','c','d'].slice();
Slice will Return a shallow copy of an array or part of an array. let arr = ['a','b','c','d']; console.log(arr.slice()); // ["a","b","c","d"] - copies array console.log(arr.slice(2)); // ["c","d"] - copies from index 2 to end console.log(arr.slice(1,3)); // ["b","c"] - copies from index 1 to 3 mnemonic: Note the SL in slice are in "ShaLlow"
If I need to find something in the string str, what's the diff between: str.search() and str.includes() and str.indexOf() ?
search and indexOf both return the index of text found or -1 if there's no match but, search() takes a regex parameter indexOf() takes a string and will be faster - it also allows for a second parameter with the start position includes() returns true or false
Explain prototype and give me a very realistic, useful example - with proper syntax. Note to self: learn this last
All JS objects inherit properties and methods from a prototype. For example, Array objects inherit from Array.prototype *Prototypes are a way JS objects can inherit features from one another*. There is also a prototype property on constructor functions. Think of it like a template that can be used with multiple instances. Example: class Rocket { color = 'red'; manufacturer = 'SpaceX'; owner = 'Elon'; } Rocket.prototype.takeoff = function() { return `3,2,1 Blastoff, bye ${this.owner}!` }; const dragon = new Rocket(); console.log(dragon.takeoff()); // 3,2,1 Blastoff, bye Elon!
What does Object.keys() return?
An array of a given object's *own, enumerable* property names, in the same order as a loop. const ship = { owner: 'elon', name: 'dragon' } console.log(Object.keys(ship)); // owner, name own: not inherited properties enumerable: loopable
What is an npm registry?
First of all, this is *where npm resolves packages by name and version*. So when you run a command like "npm i lodash" a registry has to be searched to find the package. *By default, npm is configured to use the npmjs public registry*, but a private registry can be made if you don't want the world to see your top-secret intellectual property or your crappy uncommented spaghetti code. It requires special setup and CouchDB and "private" : true in your package.json. Good luck setting that up.
What are the different ways to import?
Here: import defaultExport from "module-name"; import * as name from "module-name"; import { export1 } from "module-name"; import { export1 as alias1 } from "module-name"; import { export1 , export2 } from "module-name"; import { foo , bar } from "module-name/path/to/specific/un-exported/file"; import { export1 , export2 as alias2 , [...] } from "module-name"; import defaultExport, { export1 [ , [...] ] } from "module-name"; import defaultExport, * as name from "module-name"; import "module-name"; var promise = import("module-name");
What is debouncing?
It's a practice used to *improve browser performance*. Remember how JavaScript is single threaded? If something complex could be invoked frequently, like a handler for clicking a button, frequent execution of code or a handler can slow things down. Using timers like setTimeout() can be used to *prevent frequent execution of a function*. For example, a button click can be handled once every 2 seconds, rather than every time a button is clicked. You'll have to do a 500ms debounce for filtering table results one day, trust me, so go read about it.
What does Array.splice() do?
Splice can *remove* from an array and also *replace* it with a value between indexes. Splice occurs *in place* Syntax: .splice(startPos,length,replacementVal); Examples: splice returns the an array of what got spliced, or removed: console.log(['a','b','c','d'].splice()); // [] console.log(['a','b','c','d'].splice(2)); // ["c","d"] Splice executes in place, and the 3rd parameter will replace what gets spliced. let arr = ['a','b','c','d']; arr.splice(2,1,'z'); console.log(arr); // ["a", "b", "z", "d"] mnemonic: Note the P in splice is in "rePlace" and "in Place"
What is going on here? And what the heck am I supposed to do with it? let concatTwo = x => y => x+y;
That would be *Currying*. This example is shorthand syntax for a function within a function. The arrow function is equivalent to: function concatTwo(x) { return function(y) { console.log(x + y); } } and called like this concatTwo('hello')(' world!'); // hello world! *Currying calls a sequence of functions*. You can do cool stuff with it like get the first function result and reuse it with the nested function. let hello = concatTwo('hello'); hello(' world!'); //hello world! hello(' bukcy!'); //hello bukcy!
Please.. one more time... tell me the primitives?
There are 6 primitives string number undefined bigint boolean null symbol ...and they are *immutable* memorize it and stop asking If you can figure out how to pronounce "snubbns", you have a mnemonic. And a mnemonic for the ones you'll forget? BUNS: *bigint, undefined, null and symbol*
I have only used console.log() since 1993. I have never wondered about other things I could do in the console. I just wanted to tell you that.
Wow, thanks for sharing. But there is so much more you can do with the Console object! And so much more you *need to know for your certification*. assert() - writes an error message if assertion is *false* clear() - clears console count() - counts number of times count is called error() - outputs an error message group() - creates an inline group, indents until groupEnd() groupCollapsed() - creates an inline group, but collapsed info() - outputs an informational message log() - you know what this does table() - displays tabular data as a table time() - starts a timer timeEnd() - stops a timer started with time() trace() - outputs a stack trace warn() - outputs a warning message now open Facebook in Chrome, open the console, and check it out. Once you can make logs like that you can get your cert, resign from Netscape, work for FAANG and buy a Tesla.
Tell me about exports.
Yeah, it's a broad statement because there are lots of different ways to export and different types of exports. The export statement *exports live bindings to functions, objects or primitive values from a module* so other programs can use them with import. They are *always in strict mode* even if you don't declare it. Two types: *named, and default* There can be only one... default export. Example: export { watchHighlander as default }; export default function watchHighlander() {...} There can be multiple named exports. export {someFunction, aVariable}; export let dogName = 'Doug'; export function doSomething;
What is an appropriate syntax to get an array with only the unique items from the array below? [1,1,1,3,3,3,4,5,7,7,7]
let result = [...new Set(numbers)]
When do I want to use DOMContentLoaded vs load?
load happens after everything (stylesheets, images, and subframes). DOMContentLoaded is fired when the doc is loaded and parsed.