A *very* seasoned software engineer, I wrote my first basic game, a lunar landing game, in Basic in 1969. Currently I am doing web development in Ruby on Rails, JavaScript, Elm.
Formatting code: Prettier is something folks should know about; it integrates with ESLint quite nicely, and can be added to most code editors pretty easily.
let, const, and var: In the examples given, doing let container = document.getElementByID('SomeId') is more of a signal to the developers that it will somehow be changing. In fact, the variable itself never changes, it still points to the same DOM element object, which is changing internally. This can be hugely confusing to realize that const container = document.getElementById('SomeId') ; container.innerHTML = "Hello" will work exactly the same way. Kyle Simpson (@getify) has a great discussion on this in YDKJS.
Also, don't completely discard var from your vocabulary as it still has uses; instead understand when and where to apply it.
The adage "use const until you can't" is a good one if everyone understands that.
From early on Jelena enjoyed handling challenging obstacles and tasks. Today, she's a passionate problem-solver that loves to share her knowledge and experience.
A *very* seasoned software engineer, I wrote my first basic game, a lunar landing game, in Basic in 1969. Currently I am doing web development in Ruby on Rails, JavaScript, Elm.
A digital native focussing on design systems, brand identity and creative coding to help digital products take shape. My approach is organized, systematic and inclusive.
On formatting code: using linters is a good way to make sure your code is consistent (e.g. in a team or contributions to open-source projects) but maybe more important is to look at recommended and standard configs to see which rules a specific linter enforces.
AI Governance Architect: Regulatory Compliance & Generative Engine Risk Mitigation
A/E/C construction industry Rennaissance Man. Polymath. Professional Amateur.
Location
Milwaukee County, WI, USA
Education
UW Milwaukee - School of Architecture and Urban Planning, Bachelor's Science (1981-1986)
Work
The Architect's role in this context is to ensure the statutory licensee's license is not a risk.
Nice. I only found out about let the other week when chatting with a colleague and seeing let used frequently. I'd been a var user since I originally picked up JavaScript. It's really nice to read best practice articles.
Anyone can learn to code, but coding well, and not picking up bad habits are hugely important.
Use Prettier. All formatting should be deterministic and fixable automatically.
const vs let
I see a lot of people using let for an object when they plan to mutate it.
Even if they understand it's unnecessary and just use it as a convention it's very confusing to other developers who look for the binding being redefined further on.
Father and husband; Android Engineer and Quality Advocate; For fun, I enjoy playing video games, guitar, and table top games. I also dabble in hobby game dev and pixel arts.
From early on Jelena enjoyed handling challenging obstacles and tasks. Today, she's a passionate problem-solver that loves to share her knowledge and experience.
Formatting code: Prettier is something folks should know about; it integrates with ESLint quite nicely, and can be added to most code editors pretty easily.
let, const, and var: In the examples given, doing
let container = document.getElementByID('SomeId')is more of a signal to the developers that it will somehow be changing. In fact, the variable itself never changes, it still points to the same DOM element object, which is changing internally. This can be hugely confusing to realize thatconst container = document.getElementById('SomeId') ; container.innerHTML = "Hello"will work exactly the same way. Kyle Simpson (@getify) has a great discussion on this in YDKJS.Also, don't completely discard
varfrom your vocabulary as it still has uses; instead understand when and where to apply it.The adage "use const until you can't" is a good one if everyone understands that.
Great article!
Thank you.
I come to the comments looking for an explanation about the let/const and why we should use them instead of var.
Can you clarify the difference for me?
No where near as well as Kyle does: github.com/getify/You-Dont-Know-JS...
Came here to make literally these two comments!
DOM elements are also global when accessed by ID. e.g.
window['my_element_id']ormy_element_id#oldschoolwebthis is a big DON'T
For sure a bad idea. Just a little anecdote.
Here's some more web anecdotes
I like using snake_case for variables. It improved readability by distinguishing them form camelCasedMethods.
And when it comes to
letvsconst, useconsteverywhere until you can't.On formatting code: using linters is a good way to make sure your code is consistent (e.g. in a team or contributions to open-source projects) but maybe more important is to look at recommended and standard configs to see which rules a specific linter enforces.
Consistency is also a Best Practice. When authoring an article and suggesting that Javascript developers use:
const container = document.getElementById("someElementId");
And then later in that same article the author uses:
let container = document.getElementById("someElementId");
It can elicit the pernicious WTF response followed by an emoji :-)
Like the article says the let syntax indicates that the value is to be changed which happens in the code snippet.
Nice. I only found out about let the other week when chatting with a colleague and seeing let used frequently. I'd been a var user since I originally picked up JavaScript. It's really nice to read best practice articles.
Anyone can learn to code, but coding well, and not picking up bad habits are hugely important.
Formatting
Use Prettier. All formatting should be deterministic and fixable automatically.
constvsletI see a lot of people using
letfor an object when they plan to mutate it.Even if they understand it's unnecessary and just use it as a convention it's very confusing to other developers who look for the binding being redefined further on.
Nice :D
Because it's seems not standard :/ but you can easely recreate this idea:
document.querySelectorAll("[id]").forEach(el => window[el.id] = el);It's totally standard.
Great guide for javascript, but most thoughts apply to all languages independent of syntax.
Keep up the great work.
Just getting into js and coming from Java. Great to know about the linters and Babel!
You are welcome 💪😉