JavaScript Samples: Variables in JavaScript

Learning a programming language is fun and inspiring, but sometimes it can get challenging. This excitement is probably what inspires people all around the world to give it a try and immerse themselves in the realm of variables, commands and coding. And it’s alright if you don’t feel motivated at times – learning how to code can get really difficult and frustrating. Knowing this, we started publishing JavaScript samples to help all the struggling students out there. Sometimes a single sentence or a string of code can make you realize what your mistake was.

We wish to remind you that you are not allowed to use the information presented in our blog without proper references, or to claim the assignment to be your own, since it will be a violation of the copyright law and can profoundly impact your academic future. We can, however, offer you a better option – your very own JavaScript samples to learn from. Place your order, specify the instructions, pick the expert you liked the most and wait for work to be done. On-time delivery and top-notch quality are guaranteed! You can always check the progress of your order or chat with your expert from your personal page. Programming is difficult, but we know how to make it easy and enjoyable. Now check the sample and decide whether you need further help.

Depending on the reason for the creation of your script, you will need to work with the information.

If you are making an Internet shop, you will work with the goods and the basket. If you create the chat, you will have visitors, messages and so on.

To store information, the variables are used.

Variable

The variable consists of a name and a dedicated memory which corresponds to it. To declare or, in other words, to create a variable, the keyword “var” is used:

var message;

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

var message;
message = 'Hello'; // save a string in the variable

This data will be stored in the corresponding memory area and available in the future:

var message;
message = 'Hello!';
alert (message); // Displays the contents of a variable

You can combine the declaration of the variable and writing the data into it:

var message = 'Hello!';
It is possible to declare several variables at once:
var user = 'John', age = 25, message = 'Hello';

Variable names

There are two restrictions for variable names in Javascript:

  1. The name can consist of letters, numbers, symbols, and $ _
  2. The first character must not be a digit.

For example:

var myName;
var test123;
Such symbols as “$” or “_” are considered to be usual, like letters:
var $ = 1;
var _ = 2;

alert( $ + _ ); // 3
And such variables will be incorrect:
var 1a; // starts with a number

var my-name; //'-' is restricted
Constants
A constant is a value that never changes. They are usually called capitalized:
var COLOR_RED = "#F00";
var COLOR_GREEN = "#0F0";
var COLOR_BLUE = "#00F";
var COLOR_ORANGE = "#FF7F00";

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

Technically, the constant is a usual variable and it can be changed. We just agree not to do that.

  1. COLOR_ORANGE is an understandable name. Using #FF7F00 is more complicated.
  2. It is much more simple to make a typo in such a string as #FF7F00. In COLOR_ORANGE it would be easier to notice.

Total

In JavaScript, you can declare variables to store data. This is done using the var.

Technically, you can simply write the value of the variable and without declaration, but this is not recommended for several reasons.

Variables which are named CAPITALIZED are constants and never change. Usually, they are used for convenience.

Leave a Reply

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

Customer testimonials

Submit your instructions to the experts without charge.