JavaScript: Hello, World!
Learning a new programming language traditionally begins with a 'Hello, World!' program. This is a simple program that prints a greeting to the screen and introduces the syntax and structure of the new language.
Hello, World!This tradition is already more than forty years old, and we will start with it too. In the first lesson, we will write a Hello, World! program. In JavaScript, this program looks like this:
console.log('Hello, World!');The console.log() command prints to the screen the text specified in parentheses. Instead of the example, you can write any other text.
console.log('Hexlet - programming school');The command stays the same, only the contents of the parentheses change. So that the program understands that this is exactly text, it is enclosed in quotes. You can use single '...' or double "..." quotes, but the opening and closing quotes must match.
console.log("Hexlet - programming school");According to the style accepted in the JavaScript community, single quotes are recommended for strings. If there is an apostrophe inside the string, single quotes will break the syntax, so in such cases double quotes are used.
console.log("it's JavaScript"); // apostrophe inside, so double quotesThe meaning of symbols
Code consists of commands, and each of them must be written in a specific form. Besides letters, quotes ' and ", parentheses (), and punctuation marks are important in code. A missing or mixed-up sign will cause the program not to run. Try to determine what error was made in each of the lines?
console.log("it's JavaScript"
console.log(it's JavaScript")
consol.log("it's JavaScript")
console.log('it's JavaScript")
consolelog("it's JavaScript")Even a small difference, for example one extra letter or a different sign, can cause the program not to work. This also applies to case, that is, to the difference between uppercase and lowercase letters. While in ordinary text Hello and hello look the same, for JavaScript these are different words. JavaScript considers console.log, Console.Log, and CONSOLE.LOG to be different commands, and only the first variant will work.
Where to practice
Theory is absorbed better when you run code in parallel and see the result. The browser console (DevTools) is suitable for this, where commands are executed line by line. Everything that appears in the lesson is worth trying in the browser console.
How does this work technically? Any code that is written is passed to the JavaScript engine, which executes this code and prints the result of its work to the screen.
Code JS Engine Screen
┌──────────────────┐ ┌───────────┐ ┌──────────────┐
│ console.log(…) │──→│JavaScript │──→│ Hello, World!│
└──────────────────┘ └───────────┘ └──────────────┘Instructions
Type the code from the task into the editor character by character and click "Check".
console.log('Hello, World!');Note: if you write heLLo, woRld! instead of Hello, World!, it will be considered different text, because uppercase and lowercase letters are different characters. The size of a letter is called case, and people say: case matters! This applies to almost everything in code, so get used to always paying attention to case.
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');
await import('./index.js');
const firstArg = consoleLogSpy.mock.calls.join('\n');
expect(firstArg).toBe('Hello, World!');
});Teacher's solution will be available in:
20:00
