Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Alex Hort-Francis
Alex Hort-Francis
17,074 Points

Javascript function naming convention: capitalising HTML tag names -- best practice?

I noticed in this video (and other previous ones) that when making a function in Javascript the HTML tag name mentioned within the function name is capitalised. Ordinarily function names seem to be written in camel case.

For example, Guil writes:

function createLI(text) {
...

... as opposed to:

function createLi(text) {
...

Is this generally considered best practice when writing Javascript?

Thanks, Alex

2 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hi Alex Francis

Yes, while camelCase is the preferred practice when naming JavaScript functions, HTML tags are not individual words, so it's best to not camelCase those. Like acronyms, they are best capitalized in the naming process. This is not only seems the most common practice, but also aids in readability and clarity.

Here's another example: function randomRGB(value) {};

randomRgb looks funny, is hard to read quickly, and unclear as to the purpose of the function.

You can see this more if you have a look at some of the native functions within JavaScript itself:
.innerHTML. (HTML is capitalized).
.innerText. (Text is not capitalized).

Yes, different team will have different conventions, but capitalizing tags and acronyms is what I've seen and done the most. Even searching the web, you will find a mix of both, so like anything else with coding... a "definitive" and "absolute" answer is not found. 🤷‍♂️

Hope that helps.

Keep Coding! :) :dizzy:

Hi Alex! When declaring a function name (or any variable name) in JavaScript, it should be written in camel case. In a real-world setting, it actually depends on your team on what style guides in programming you are going to use. As per standard JavaScript style guide, you can read more here: https://www.w3schools.com/js/js_conventions.asp. Happy coding!