r/JavaScriptTips • u/[deleted] • Feb 20 '22
Learning Javascript, trying to understand camelCase. Why does capitalization matter in the left side of the equation, but not in the right? Example included.
Learning Javascript, trying to understand camelCase. Why does capitalization matter in the left side of the equation, but not in the right? Example included.
Trying to learn Javascript and using FreeCodeCamp's example. Having a difficult time learning how capitalization is applied and why it matter in some instances, but not others.
See example problem below!
// Setup
const lastName = "Lovelace";
// Only change code below this line
const secondToLastLetterOfLastName = lastName[lastName.length - 3]; // Change this line
If the const is defining Lovelace as forever and it will never change, doesn't that mean I have to write lastName as intended too, with the lower level "L"?
Why is in the second const, I have to capitalize the L in Last? That would make it not constant or const, no?
Unless the rules don't apply, and it's camelCase only to the right side of equation? but in the left, it has to follow camelCase? Or does camelCase always apply everywhere?
We are defining lastName as a const, so shouldn't it also be lastName and NOT LastName?
2
u/mao034 Feb 20 '22
OP Don't get confused by that, it's very simple; you can name your variables and constants however you want, camel case is just a popular naming convention you can follow, the name of the variable is just an identifier to differentiate it from others.
All of these are valid names for example;
const mYcOnsTaNt = 1; let my_variable = 2; my_variable = 5; const sum_Result = mYcOnsTaNt + my_variable;
// camel case naming convention: const myConstant = 1; let myVariable = 2; myVariable = 5; const sumResult = myConstant + myVariable;
Just make sure you spell it the same way as you defined it when you want to use it.