
Naming Variables in JavaScript
20 March, 2024
40
40
0
Contributors
There are a few rules that we must follow to ensure that we don't break our code.
- There can be no spaces or dashes in our code names.
- Names like
let first name = ...
orlet first-name = ...
are not viable.
- Names like
- Names must begin with a letter of the alphabet ->
a-z, A-Z
. - Best practice in JS is to start your variables with a lowercase letter e.g.
let name = ...
as opposed tolet Name = ...
.
- Best practice in JS is to start your variables with a lowercase letter e.g.
- For multi-word variables, we use either the camelCase or snake_case.
- camelCase capitalizes the first letter of each word in a phrase, without using spaces or punctuation (with exception to the first word).
- snakecase is a way of writing phrases without spaces, where spaces are replaced with underscores ``, and the words are all lower case.
So the correct way to write the first name example above would be:
let firstName = "Arnob"
orlet first_name = "Arnob"