Learn variables and types in programming

Codistwa

Learn Variables and types

in Programming

Reading time : 5 min

Last update : February 11th, 2022

When we talk about variables, we think about values and types. There are strongly typed languages like C or Java and weakly / dynamically typed languages like JavaScript or Python. There is terminology to respect for naming. You must not use reserved words, i.e., words already used by the programming language (e.g. Boolean); in this case, the program will return an error.

In JavaScript, to check a type, we use typeof().

In Python, to check a type, we use type().

Half off everything 2manning

Definition of a variable

A variable has a name, a type, and a memory address (memory area where the information is stored). In addition, a variable will contain integers, decimal numbers, strings of characters (texts), etc.

When we want to put a value in a variable, we proceed to an affection. The assignment is read from right to left. Affection is done with the = operator.

Here the variable is represented by a wagon because it is a container. So we can put different types like a number, for example, the number 6. Finally, the number 6 is placed in the wagon, which is, therefore, the container.

Code

javascriptjavascript
1const welcome = 'Hello';
2console.log(typeof(welcome)); // string
3

Types

A strongly typed programming language means that you define explicitly the variable type during the declaration. A weakly typed / dynamically typed programming language means great flexibility when declaring variables. Indeed, the type is not declared in hard; the program will determine it automatically during the execution.

The types of JavaScript are pretty particular because it is a mixture between the so-called Primitive types and the Object type.

The primitive types : Number, String, Boolean, Null, Undefined and Symbol The Object type.

Python's types are: numeric, sequences, dictionaries, classes, instances and exceptions.

C and C++ have about 30 types: char, float, int double, long int ...

Several variables can be used, numbers represented by the number 2, strings represented here by the letters a, b, c. Finally, an array can contain several values, here represented by different numbers and letters.

Code

javascriptjavascript
1const welcome = 'Hello';
2let length = 2;
3const isHidden = false;

Concatenation

You can join several strings together using what is called concatenation. Sometimes you have to convert the type yourself for this to work; otherwise, the language will automatically do it. This is useful to create a sentence, for example.

The values are in two different wagons, so two different variables, D and RY. They can be assembled, here illustrated with the plus. In the last comic strip, we find the DRY values assembled in a wagon, a single variable.

Code

javascriptjavascript
1const presentation = 'I have';
2let age = 32;
3
4console.log(presentation + ' ' + age); // I have 32
5console.log(`${presentation} ${age}`); // I have 32

Mutation

Depending on the type, you can sometimes change the value; this is called mutation. This is not the case for constants. Preferably use constants to avoid accidental overwriting of the variable's value.

Two wagons are present, the left wagon has the letter R, and the right wagon has the number 2. It is a constant (the number C is written), so we can not change the wagon. In the second comic book panel, the content of each wagon is changed, so the left wagon has the letter D, and the right wagon has the number 6. However, the first wagon has no problem in the last comic strip, but the second one does. It has a red cross because it is a constant.

Code

javascriptjavascript
1const welcome = 'Hello';
2let length = 2;
3
4welcome = 't';
5length = 3;
6
7console.log(welcome); // error
8console.log(length); // no error

Conversion

You can change the type of a variable. In this case, we say that we cast the variable. This can be useful, but in general, it is better to avoid it. The NaN (Not A Number) error will appear when the conversion has failed, especially with numbers. For example, in JavaScript, you can convert a string with an int if the string is marked '5' but not 'five' using the ==.

In the first comic strip, the number 3 is represented in several forms, colors, or three small points. In the second comic strip, we try to put together two numbers in the same shape. Finally, in the last comic strip, we see that the numbers in two different forms can not be assembled, so a red cross represents an error.

Code

javascriptjavascript
1console.log('3' + 2); // 32
2console.log('3' - 2); // 1
3console.log('five' * 2); // NaN

Scope

The location of a variable is important because, depending on the location, it will be accessible or not by a function. This is called the scope of the variable. There are two types of scope :

  • the global scope : accessible everywhere in the program
  • the local scope : accessible only in a function

The best practice is to use primarily local variables, but this is not always possible. In which case, you have to be careful to give different names to avoid an overwritten value or program error (for example, with a constant).

Here, three types of variables are represented: the numbers 1 and 2, and the letter D. These variables are placed in boxes, with the letter R created in the first box. The second box tries to recover the letter R., But as we can see in the last comic strip, this is impossible, so there is a red cross.

Code

javascriptjavascript
1const doors = 5; // global
2
3function getDoors() {
4  const bathrooms = 1; // local
5  console.log(doors);
6}
7
8function getBathRooms() {
9  console.log(bathroom); // error
10}
11
12console.log(getDoors()); // 5
13console.log(getBathRooms()); // undefined

Naming conventions

You will probably work with other developers, so it's crucial to name variables explicitly. You have to use actual words, for example, not just letters. Moreover, depending on the language and variable type, the standard uses camelCase, PascalCase, or snake_case. These terms sound exotic, but if you look closely, you can tell the usage by how they are written.

  • camelCase : myVariable

  • PascalCase : MyVariable

  • snake_case : my_variable

For Python, for example, the good practice is to use the snake_case while JavaScript uses the camelCase or PascalCase.

To return to the convention, we will pay attention to the type to find the right name. It is essential to know that it can be challenging to find a correct name, so don't get discouraged. Use English to stay consistent with the rest of the language.

  • for a boolean, we will define the name of the variable concerning its state (true / false)

Code

javascriptjavascript
1// bad
2const str = 'Marie';
3// good
4const firstName = 'Marie';
5
6// bad 
7const zero = 0;
8
9// good
10const total = 0;
11
12// bad
13const open = true;
14const hidden = false;
15
16// good
17const isOpen = true;
18const isHidden = false;
Print booksmanning

Variables are part of the essence of programming. There are always types, but depending on the language, they are assigned dynamically or hard-coded. They have a scope, so you have to be careful where they are created to avoid collisions. They can be modified, concatenated, cast. The programming language reserves words, so they cannot be named anyway, or an error will be displayed. Therefore, it is essential to respect the naming conventions.

Ressources

Source code