JavaScript: Data types
Programs work with different kinds of information: text, numbers, dates, boolean values. Every value in a program has a type.
For example:
'Hello, World!'is a string (string);7,-198,0,3.14are numbers (number);trueandfalseare boolean values (boolean).
What is a data type?
A data type defines:
- which values belong to it;
- which operations can be performed with it.
For example, numbers can be added, divided, and multiplied. Strings are combined differently — using concatenation. Multiplying a string by a string makes no sense:
// Nonsense: 'mom' * 'notebook'Numbers and strings belong to different types
An example of printing a number:
console.log(5); // => 5
console.log(-5); // => -5An example of printing a string:
console.log('5'); // => 5
console.log('-5'); // => -5On the screen the result looks the same, but inside the program these are completely different things:
| Value | Data type |
|---|---|
5 | number (number) |
'5' | string (string) |
Numbers in JavaScript
In many languages, integers and fractional numbers are different types (for example, in Python they are int and float). JavaScript has no such division: both integers and fractional numbers belong to a single type — number.
console.log(10.234); // => 10.234
console.log(-0.4); // => -0.4
console.log(3.5 + 1.2); // => 4.7
console.log(5 / 2); // => 2.5
console.log(2.75 - 0.5); // => 2.25Primitive types
Types like string, number, boolean are called primitive — they are built directly into the language.
JavaScript primitive types
├── number : numbers (integer and fractional) (7, -3, 3.14)
├── string : strings ('hello')
├── boolean : boolean type (true, false)
├── null : intentional absence of a value
└── undefined : value is not setIn addition to strings and numbers, JavaScript has the boolean type boolean with the values true and false, as well as the special values null and undefined. We will encounter them in more detail in the future.
How to find out the type of a value
The typeof operator returns the type as a string:
console.log(typeof 42); // => 'number'
console.log(typeof 'hello'); // => 'string'
console.log(typeof true); // => 'boolean'
console.log(typeof undefined); // => 'undefined'
console.log(typeof null); // => 'object' (a historical JS bug)There are also composite types — arrays, objects, and others. We will get acquainted with them later. Moreover, in JavaScript you can create your own types (for example, classes), but to start with it is important to understand primitives well.
Instructions
The current year is already defined in the program:
const currentYear = 2026;Write a program that prints profile data in the following format:
Name: Anna
Birth year: 1994
Age: 32
Rating: 4.7To do this:
- create a variable with the birth year;
- calculate the age based on the current year;
- print the data on the screen with labels on the left.
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('Name: Anna\nBirth year: 1994\nAge: 32\nRating: 4.7');
});Teacher's solution will be available in:
20:00
