JavaScript Variables

Variables in JavaScript

JavaScript Variables Tutorial from Coding compiler. Depending on what you are doing the script, you will need to work with the information. If this is an electronic store, then this is a goods basket. If the chat – visitors, messages and so on.

Variables are used to store information .

JavaScript Variable

A variable consists of a name and a dedicated area of ​​memory that corresponds to it.

To declare or, in other words, create a variable, use the keyword var:

var message;

After the declaration, you can write to the variable data:

var message;
message = ‘Hello’; // save variable string

This data will be stored in the corresponding memory area and will be available later on by name:

var message;
message = ‘Hello!’;

alert( message ); // displays the contents of the variable

For brevity, you can combine variable declaration and data entry:

var message = ‘Hello!’;

You can even declare several variables at once:

var user = ‘John’, age = 25, message = ‘Hello’;

Analogy from life

The easiest way to understand a variable is to present it as a “box” for data, with a unique name.

For example, a variable message is a box in which the value is stored “Hello!”:

You can put any value in the box, and later change it. The value in a variable can be changed as many times as necessary:

var message;

message = ‘Hello!’;

message = ‘World!’; // replaced value

alert( message );

When the value changes, the old content of the variable is deleted.

You can declare two variables and copy data from one to another:



var hello = ‘Hello world!’;

var message;

// copied value
message = hello;

alert( hello ); // Hello world!
alert( message ); // Hello world!

On a note:

There are functional programming languages ​​in which the value of a variable cannot be changed. For example, Scala or Erlang .

In such languages, once put the value in the box – and it is stored there forever, neither deleted nor changed. But you need to save something else – if you please, create a new box (declare a new variable), reuse is impossible.

In appearance, it is not very convenient, but, oddly enough, it is quite possible to successfully program in such languages. Moreover, it turns out that in a number of areas, for example in parallelizing calculations, they have advantages. The study of some functional language is recommended to expand the horizons.

Variable names

There are only two restrictions on the name of a variable in JavaScript.

  1. The name may consist of: letters, numbers, symbols $and_
  2. The first character must not be a number.

For example:

var myName;
var test123;

What is particularly interesting is that the dollar ‘$’and underscore characters ‘_’are just as common as the letters:

var $ = 1; // declared a variable named ‘$’
var _ = 2; // variable named ‘_’

alert( $ + _ ); // 3

And such variables would be wrong:

var 1a; // the beginning can not be a figure

var my-name; // hyphen ‘-‘ is not a valid character

Case of letters matters

Variables apple and AppLE- two different variables.

Reserved names

There is a list of reserved words that cannot be used for variables, as they are used by the language itself, for example: var, class, return, export etc.

For example, this example will produce a syntax error:

var return = 5; // mistake
alert(return);

Importance of the var directive

In the old standard, JavaScript was allowed to create a variable and without var, simply assigning it a value:

num = 5; // the num variable will be created if it was not

In the mode, “use strict”this is no longer possible.

The following code will generate an error:


“use strict”;

num = 5; // error: num is not defined

Note that the directive use strict must be placed before the code, otherwise it will not work:



var something;

“use strict”; // too late

num = 5; // there will be no error since strict mode is not activated

Error in IE8- without var

If you are going to support IE8-, then I have another reason for you to always use var.

The following document in IE8 will not output anything, there will be an error:


<div id=”test”></div>
<script>
 test = 5; // there will be a mistake here!
 alert( test ); // will not work
</script>

This is because the variable is testnot declared through varand matches the id element <div>. Don’t even ask why – this is a bug in IE browser before version 9.

The most “funny” is that such an error in assigning values ​​will be only in IE8 – and only if there is an element with on the page id that matches the variable name.

Such errors are especially “fun” to correct and debug.

The conclusion is simple – always declare the variables through var, and there will be no surprises. Even in old IE.

Constants

A constant is a variable that never changes. As a rule, they are called in capital letters, underlined.

For example:

var COLOR_RED = “#F00”;
var COLOR_GREEN = “#0F0”;
var COLOR_BLUE = “#00F”;
var COLOR_ORANGE = “#FF7F00”;

var color = COLOR_ORANGE;
alert( color ); // #FF7F00

Technically, a constant is a regular variable, that is, it can be changed. But we agree not to do this.

Why do we need constants? Why not just write var color = “#FF7F00”?

  1. First, the constant COLOR_ORANGEis a friendly name. By assigning var color=”#FF7F00″it is not clear that the color is orange. In other words, the constant COLOR_ORANGEis an “understandable pseudonym” for the value #FF7F00.
  2. Secondly, a typo in the string, especially as complex as #FF7F00it may be, is not seen, and in the name of a constant it is much more difficult to admit it.

Constants are used instead of lines and numbers to make the program clearer and to avoid errors.

Summery:

  • In JavaScript, you can declare variables for data storage. This is done with help var.
  • Technically, you can simply write the value without declaring a variable, but for a number of reasons it is not recommended.
  • At the ad can immediately assign the value: var x = 10.
  • Variables that are named БОЛЬШИМИ_БУКВАМИare constants, that is, they never change. As a rule, they are used for convenience so that there are fewer errors.

JavaScript Variables Tasks 

Work with variables in JavaScript

  1. Declare two variables: adminand name.
  2. Write in name line “JavaScript Variables”.
  3. Copy value from name to admin.
  4. Output admin(should output “Vasily”).

Related JavaScript Tutorials For Beginners

JavaScript Introduction Tutorials
Introduction to JavaScript
Javascript Reference and Specifications
Javascript Code editors
Javascript Developer Console
JavaScript Basics Tutorial
JavaScript Hello World.!
External JavaScript Files
JavaScript Code Structure
Use Strict in JavaScript


Leave a Reply

Your email address will not be published. Required fields are marked *