Variables

Variable Declaration

Variables are elements created during the execution of the program and used to store data. When creating variables, their names and the type of data they will store are determined. This name will be used when the variable will be given a value, or when it is desired to reach the value stored by the variable.

Variables are constructs found in all programming languages, and when defining (creating) a variable, it must be specified what type of data it will store. For example, if we define a variable to store numbers, we cannot then assign text to that variable.

The difference between JavaScript and other languages ​​is that the variable can be declared without specifying the type. Variables can be created by specifying only the name of the variable with the var or let command . Then, the desired type of data can be entered into this variable. The type of the variable will be detected according to the entered value.

Examples:

var number1;

number1=100;

let name="Victory";

let number2=50;

Note: We can use single quotes or double quotes when assigning values ​​to variables of type String.

Difference of Var and Let Commands
 
A variable or object defined with the var command is considered global. The let command, on the other hand, does not create a global variable.
 
In addition, the variable defined in a function with the var command can also be created in another function with the same name and can be used independently of each other.

Data Types

We can talk about 3 different data types for variables in JavaScript: 

Number : It is used to store all kinds of numeric values. In decimal numbers, a period is used to separate the decimal part.

String : Used to store characters or text.

Boolean : They can store a true or false value.

Note: JavaScript itself detects the types of variables based on the data entered. For example, when a numeric value is assigned to a variable, that variable changes to Number. If the same variable is given a text value later, that variable is automatically converted to string type. We don't need to do a conversion.

E.g;

var a=50;

a="Ethan";

This example would cause error in other languages. But JavaScript will not be a problem. A different type of data can be assigned to the variable than the one originally assigned.

Variable Naming Conventions

JavaScript is a case sensitive language.

When naming variables, non-English characters (  ı, İ, ğ, Ğ, ü, Ü, ş, Ş, ö, Ö, ç, Ç  ), spaces and special characters ( . , ; : / etc.) should not be used.

In addition, words that have another meaning in the programming language should not be chosen as variable names. (int, not, if, char etc.)

Variable names cannot begin with numbers or numbers. A variable can be named not1 but not 1not.

Mathematical operations between variables

  • If two variables of type String are added together, the values ​​of the two variables are combined side by side.

var x = "computer" + " lecture";  result  computer lesson

  • All kinds of mathematical operations can be performed between variables of type Number.
  • When a string and a variable of type number are added together, operations are performed from left to right. Let's explain this with different examples:

var x = "computer" + 15;  result computer15

var x = 15 + "computer";  result 15computer

var x = 15 + "computer" + 15;  result 15computer15

var x = 15 + 5 + "computer" + 15;  result  20 computers15

var x = "computer" + 15 + 5;  result  computer 155

var x = 15 + 5 + "computer" + 15 + 5;  result 20computer155

  • While performing numerical operations with Boolean type variables, the operation is performed by accepting the True value as 1 and the False value as 0.
  • When performing operations between Boolean type variables and string type variables, "True" for True value and "False" for False value are added as text.

typeof operator

We can use the typeof operator to find out the type of variables or expressions .

var variable1 = "computer";

var variable2 = 35;

document.getElementById("box1").innerHTML = typeof variable1;  result  string

document.getElementById("box1").innerHTML = typeof variable2;  result  Number

document.getElementById("box1").innerHTML = typeof  "car";  result string

document.getElementById("box1").innerHTML = typeof 35;  result Number;

parseInt() Method

When we try to add two numbers with JavaScript, we may encounter a problem in some cases, especially if we get the numbers from the form elements on our page.

That is, we say add two numbers, but the program concatenates the two numbers side by side. This is because the data in the variable is perceived as a string.

We can use the parseInt method to circumvent this problem.

The parseInt method converts string type data to numeric data type.

Sample:

var a=parseInt(document.getElementById("box1").value);

In this example, the data in the text box with the id box1 is set to variable a as a number.

The parseFloat method, which is similar to the parseInt method, can be used for decimal numbers. Decimal part is discarded when parseInt method is used, decimal number can be obtained by using parseFloat method.

using javascript variables, javascript var command variable examples, javascript variable types types, how to define javascript variable

EXERCISES

Undefined, empty and null states in variables

Undefined, empty and null states in variables

If a variable is defined but no value is assigned, it is undefined

var a;

If a null value is assigned to the variable, it is considered empty.

var a = "";

A nullable variable is also empty, but its type is object.

a = null;

 

Example: Numerical operation with Javascript variables Try It

In the example, the data entered in the text box is converted into a number by means of the parseInt method, and then its square is calculated and the result is printed in the b tag.

You can see the codes, change them and see the result by clicking the Try It Yourself button.

Example: Operation with Javascript variables Try It

In the example, the data entered in the two text boxes is combined and a space is placed between them and is printed inside the b tag.

You can see the codes, change them and see the result by clicking the Try It Yourself button.



COMMENTS




Read 511 times.

Online Users: 537



javascript-variables