Javascript — Storage Wars

David Gasch
Nov 3 · 3 min read

How to best store your stuff

Using arrays and/or objects allows you to access stored information without repeating your code numerous times. You can group a number if items and access them within the array or determine whether an item is included in a specific group. When writing functions that are more complex, it is helpful to have specific sets of items grouped together for easy access.

The “use strict” designation in Javascript requires exact syntax and types when writing code. In general, Javascript reserves the ability to make assumptions when translating your code. For example, if you are using a number and expect it to react with another number, but type the number as a string, Javascript may assume you meant to use another number and return the expected result. Though it can be nice to have JS get your back and cover your tracks, there are several benefits to “use strict.”

Advantages

  1. Requires you to write cleaner, more intentional code
  2. Reduces risk of errors between users or broken code as your project develops further
  3. Keeps you from writing in efficient code
  4. Eliminates ‘silent errors’ by changing them to ‘throw errors’

Disadvantages

  1. Reduces flexibility using various Javascript types together
  2. Limits freedom in how Javascript can be written

Hoisting is another funky way to organize your code. It allows you to declare a function early in your code and then define and call it later in your code. It essentially loads a function into the page and leaves it available for use. Rather than declaring a function within the scope of itself, it allows more flexibility. Generally a function can only be run after it is defined. However by hoisting the declaration to load with the code you can declare it and then define it as needed. The main problem with doing this too much, though, is it can slow down the page because it is storing the items within the browser.

The web is a deeply complex place- and a number of entities have created their own way of doing things or creating their space within the industry. Without a set of somewhat coherent standards it would be an even more complex industry to work in. Many companies (ahem, Apple) have taken some liberties in the way their product work to develop a level of autonomy and exclusivity. Still, they must adhere to industry standards to remain relevant in the field. This is where standards bodies come into play. It is important for independent entities to establish and maintain industry standards. Though many browsers or devices are not fully compatible with various tools or components, there is still an underlying set of standards that ensure the web offers a cohesive experience. Without the establishment of standards, companies can accrue too much power in determining how media is consumed- and can force their own standards simply by bullying the market. We still see this in small ways, but the existence of these standards contribute to an approachable and organized web.

To keep projects maintainable I took some notes from my experience in Bootstrap. The ability to define classes, or even declare variables/functions allows me to easily add or remove labels to easily alter the output. Bootstrap is great because you can simply attach a class to entirely change the look or function of an element. In this way, it is possible to create a project in a way which allows for the quick and efficient manipulation of the underlying code.

Though Javascript offers built-in objects that can provide ease of use, extending them can cause a number of issues. 1) You can cause a collision where your version of an object may clash with the built-in version. 2) Some things shouldn’t be messed with. You can use your own judgement, but some actions cause way more potential for issues than they are worth. 3) Your code may not play well with others. Don’t risk breaking someone else’s toys.

Global scope in Javascript can be problematic. There are a ton of built in tools, but altering this can cause issues by creating contradictions in a single page of code. Your project can be much more robust if you try to avoid the global scope and build your code and variables in a local environments. Remember, the web is a complex place and in general it is best to limit your impact and risk of breaking something.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade