JavaScript: Naming styles
greeting is an example of a simple and clear variable name. But often names like name, email or price are not enough. For example, you may need to describe a user's name, the total number of orders, or the maximum length of a message. Such names already consist of several words. What will a variable name look like in this case?
Different programming languages use different naming styles. This determines how a variable name made of several words will look. For example, here is how you can write a variable that stores the maximum length of a message:
maxmessagelengthmaxMessageLengthmax-message-lengthmax_message_length
Main styles
Here are a few popular approaches to writing compound names:
-
kebab-case: words are separated by a hyphen —
max-message-length.It doesn't work in JavaScript, because the hyphen (
-) is interpreted as the subtraction operator. -
snake_case: words are separated by an underscore —
max_message_length. This is the standard in some other languages, for example in Python. -
CamelCase (or UpperCamelCase): each word starts with a capital letter, with no separators —
MaxMessageLength. In JavaScript, this is how classes are usually named. -
lowerCamelCase: the same thing, but the first word starts with a lowercase letter —
maxMessageLength.
How to do it right in JavaScript
The standard for variables in JavaScript is lowerCamelCase: the first word is in lowercase letters, and each subsequent one starts with a capital letter.
const userName = 'Daenerys';
const maxLength = 280;
const totalOrdersCount = 17;How not to do it
You shouldn't include the data type in a variable name. Such names are harder to read and quickly become outdated. For example, userNameString or messagesNumber describe not the meaning of the variable but its technical implementation.
A name should answer the question "what is stored?", not "what type is it?". That's why it's better to write userName instead of userNameString, and messagesCount instead of messagesNumber.
Instructions
Write a program that calculates and prints the cost of a purchase to the screen.
Create two variables in lowerCamelCase style:
- number of items:
20 - price of one item:
100
Print their product.
Tips
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.
It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.
In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.
It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.
By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.
Создавать обучающие материалы, понятные для всех без исключения, довольно сложно. Мы очень стараемся, но всегда есть что улучшать. Если вы встретили материал, который вам непонятен, опишите проблему в обратной связи нашего сообщества
Your exercise will be checked with these tests:
// @ts-check
import { expect, test, vi } from 'vitest';
test('hello world', async () => {
const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
await import('./index.js');
const firstArg = consoleLogSpy.mock.calls.join('\n');
expect(firstArg).toBe('2000');
});Teacher's solution will be available in:
20:00
