Skip to main content

Variables

@serverSerrverlesskiy

Variablesโ€‹

Variables ๐Ÿ”” (Variables, abbreviated as var) are a container, for values such as numbers used in addition โž•, or a string that we could use as part of a sentence, and other data types that we'll get to know later.

Container

Variable declarationโ€‹

Announcement

To use a variable, you must first create it, or, more precisely, declare a variable. To do this, we introduce the keyword var followed by the name you want to give your variable. The instruction below creates (in other words: declares or defines) a variable named "message":

Live Editor
Result
Loading...

Here we create a variable ๐Ÿ”” message. Currently โฑ๏ธ it does not contain a value, to be more precise, the variable contains an empty string.

Assigning a value to a variableโ€‹

Memory

Once the variable ๐Ÿ”” is declared, it can be assigned a value. To do this, write ๐Ÿ–Š๏ธ variable name ๐Ÿ””, followed by an equal sign =, followed by the value you want to assign. For example ๐Ÿ‘‡:

Live Editor
Result
Loading...

In RESULT, the value that you assigned to the variable ๐Ÿ”” returned in the console. Play around ๐ŸŽฎ with the values of a variable ๐Ÿ””, for example, complete the expression with your name.

For brevity, you can combine the declaration of a variable ๐Ÿ”” and writing ๐Ÿ–Š๏ธ in one line ๐Ÿ‘‡:

Live Editor
Result
Loading...

Variable updateโ€‹

Update

One of the peculiarities of variables ๐Ÿ”” is that their meaning can change. When a value is assigned to a variable ๐Ÿ””, you can change (update) that value simply by specifying a different value. Let's take a look at a simple example ๐Ÿ‘‡:

Live Editor
Result
Loading...

Another feature of variables ๐Ÿ”” is that they can contain almost anything, not just strings and numbers. Variables ๐Ÿ”” can also contain complex data and even entire functions. You will learn more about this as you study the course further.

::: tip Note! We say that variables contain values. This is an important distinction. Variables are not the values themselves! They are containers for values. Imagine that they are like little cardboard boxes๐Ÿ“ฆ in which you can store things. :::

Variables

Hello World

Variable naming rulesโ€‹

Rules

You can name the variable ๐Ÿ”” whatever you like, but there are limitations. Generally, you should only stick to Latin characters (0-9, a-z, A-Z) and the underscore character.

  • The use of other symbols is not recommended because they may cause errors or be incomprehensible to an international audience.
  • Do not use underscores at the beginning of variable names ๐Ÿ”” - this is used in some JavaScript constructs to denote specific things.
  • Do not use numbers at the beginning of variables ๐Ÿ””. This is invalid and will result in an error.
  • It is generally accepted to stick to the so-called "lower camel case" (camelCase - so called because of the "humps" that form the first letters of words), where you glue several words, using lowercase letters for the entire first word, and then capital letters of subsequent words. We have used this for our variable names ๐Ÿ”” in this article.
  • Make variable names ๐Ÿ”” intuitive about what data they contain. Do not use only single letters / numbers or large long phrases.
  • Variables ๐Ÿ”” are case sensitive, so myage and myAge are different variables ๐Ÿ””.
  • Last but not least - you also need to avoid using JavaScript reserved words as variable names ๐Ÿ”” - by that we mean the words that make up the actual JavaScript syntax! Thus, you cannot use words like var, function, let, and for for variable names ๐Ÿ””. Browsers recognize them as different pieces of code and therefore errors occur.

List of reserved wordsโ€‹

Reserved

We cannot call variables ๐Ÿ”” with these words, since they are reserved in the JavaScript language. break, case, catch, class, const, continue, debugger, default, delete, do, else, export, extends, finally, for, function, if, import, in, instanceof, new, return, super, switch, this, throw, try, typeof, var, void, while, with, yield

Loose typingโ€‹

Freedom

JavaScript is a โ€œfreely typed language๐Ÿ‘…โ€ which means that unlike some other languages๐Ÿ‘… you do not need to specify what type of data the variable will contain (eg numbers, strings, arrays, etc.).

For example, if you declare a variable ๐Ÿ”” and assign a quoted value to it, the browser will treat the variable ๐Ÿ”” as a string ๐Ÿ‘‡:

Live Editor
Result
Loading...

Deprecated "var" keywordโ€‹

Old

Usually var is not used in modern scripts, but it can still be hidden in old ones. This is due to the fact that it does not behave unambiguously, so instead of var we will use let for variables ๐Ÿ””, and const for constants - constants.

๐Ÿ”” The break is over, let's run to the next lesson!

Problems?โ€‹

Problem

Write to Discord chat.

Questions:โ€‹

Question

For whom does the console.log command contain information?

  1. User
  2. Developer
  3. JavaScript interpreter

What are variables?

  1. Containers for values
  2. Variable values
  3. Latin letters

What can variables contain?

  1. Only strings and numbers
  2. Numbers, strings, complex data, functions
  3. Only complex data and functions

How to write a command to assign a variable?

  1. var
  2. var [variable name] =
  3. var [variable name]

How do I update a variable?

  1. The variable cannot be updated
  2. Specify a different value for the variable
  3. Set a custom command

What is missing in the variable naming rules?

  1. Don't use numbers at the beginning of variables
  2. Don't use reserved words
  3. Stick to Latin characters

How do I write the value of a variable so that the browser treats the variable as a string?

  1. Without quotes
  2. In quotes
  3. In brackets

Which keyword don't we use to define variables?

  1. let
  2. const
  3. var

In order to understand how much you learned this lesson, take the test in the mobile application of our school on this topic or in our telegram bot.

JS Camp

  1. MDN web docs
  2. Code for Teens: The Perfect Beginner's Guide to Programming, Volume 1: Javascript - Jeremy Moritz
  3. JavaScript.ru

Contributors โœจโ€‹

Thanks goes to these wonderful people (emoji key):


Dmitriy Vasilev

๐Ÿ“–

Resoner2005

๐Ÿ› ๐ŸŽจ ๐Ÿ–‹

Become a Patron!