DEV Community

Cover image for Variables: Data Storage and Information Organization
Ana Beatriz Valentim
Ana Beatriz Valentim

Posted on

Variables: Data Storage and Information Organization

Level: Beginner | Stack: Frontend and Backend | Type: Dictionary


A variable is a space in the computer's memory reserved to store data that can be used and modified during the execution of a program. They solve the problem of value memorization, allowing the developer to use user-friendly names to manipulate complex or dynamic information.


Variable Types and Data Types

In development, every language has its own way of handling data. While the core concepts are similar (numbers, text, booleans), the nomenclatures and typing vary significantly.

JavaScript (and TypeScript)

JavaScript is known for its dynamic typing, but TypeScript adds rigor to these types.

  • Number: Represents both integers and decimals (floating point).
  • String: Sequences of characters used for text.
  • Boolean: Logical values, either true or false.
  • Object: Collections of complex data or entities.
  • Array: An ordered list of values.
  • Null / Undefined: Represent the absence of a value or an uninitialized variable.

Python

Python focuses on readability—you don't need to declare the type explicitly as it is inferred automatically.

  • int: Integers of any size.
  • float: Numbers with decimal places.
  • str: Character strings (text).
  • bool: Boolean values (True or False).
  • list: Mutable, ordered collections of items.
  • dict: Dictionaries that store key-value pairs.
  • tuple: Immutable sequences (cannot be changed).

Java

Java is a strongly, statically typed language that differentiates between "primitive" types and "objects."

  • int: Standard 32-bit integer.
  • double: High-precision decimal numbers (64-bit).
  • boolean: Stores true or false.
  • char: A single Unicode character (e.g., 'A').
  • String: (Object) Sequence of characters.
  • long: Long integers for very large numbers.

C# (C Sharp)

Widely used for game development (Unity) and enterprise systems; it follows a similar path to Java.

  • int: Integer numbers.
  • float / decimal: For monetary values or precise scientific calculations.
  • string: Texts.
  • bool: True or false.
  • var: A keyword that lets the compiler deduce the type (though the type remains fixed).

C++

A low-level language that offers total control over memory.

  • int: Integers.
  • float: Floating point.
  • double: Double-precision floating point.
  • char: Individual character.
  • bool: Boolean.
  • void: Represents the absence of a type or value (common in functions).

Typing Comparison

  • Dynamic Typing (JavaScript, Python)

    • Definition: The variable type is defined automatically during program execution.
    • Advantage: Offers greater speed and flexibility when writing code.
    • Example: x = 10 (no need to specify it's a number).
  • Static Typing (Java, C#, C++)

    • Definition: The variable type must be explicitly declared while writing the code.
    • Advantage: Ensures better security against errors and higher performance.
    • Example: int x = 10; (it is mandatory to state that the box holds an integer).

[Image comparing static vs dynamic typing in programming]


Use Cases

  • Storing the name of a user logged into the system.
  • Saving the result of a mathematical calculation for later use.
  • Controlling states in an application, such as knowing if a button was clicked.
  • Maintaining global settings that can be updated in a single place.

Practical Example

let userName = "Ana";
const minimumAge = 18;
let accountBalance = 500.00;

accountBalance = accountBalance - 50;
Enter fullscreen mode Exit fullscreen mode

Code Explanation

  • let defines a variable that allows for value reassignment during the code.
  • userName is the identifier storing the character string "Ana".
  • const creates a constant whose value cannot be changed after the initial definition.
  • accountBalance stores a decimal numerical value that is updated in the following line.
  • The assignment operator, signaled by the equals sign, transfers the data to the variable's memory space.

Advantages

  • Code reusability by referencing the same value in multiple locations.
  • Improved readability, as well-chosen names describe what the data represents.
  • Ease of maintenance, allowing a value to be changed in just one line.

Disadvantages

  • RAM consumption, since each variable occupies space in the system.
  • Possibility of scope errors, where a variable might be inaccessible or improperly overwritten.
  • Processing overhead in cases of excessive creation of global variables.

Tip

In JavaScript and TypeScript, adopt const as your default choice when declaring variables, as it reinforces reference immutability and contributes to safer, more predictable code. Recorra ao let only when there is an explicit need to reassign the value. This practice reduces side effects and improves the clarity of intent in the code, while correctly utilizing the scope control offered by these languages.


Let's Connect!

LinkedIn: linkedin.com/in/ana-beatriz-valentim

GitHub: github.com/AnaProgramando

Medium: medium.com/@ana-beatriz-valentim

Hashnode: ana-beatriz-valentim.hashnode.dev

Dev.to: dev.to/ana-beatriz-valentim

Have a question or want to see a specific topic in the next articles? Send your suggestions and let me know what you thought!


Keywords

Programming variables | data types | memory storage | let vs const | programming logic for beginners | software development foundations

Top comments (0)