Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Declare a Variable: Difference between revisions

From Groundhog Learning
Updated via bot
 
m Updated via bot
 
Line 1: Line 1:
<span id="declare-a-variablevariable"></span>
<span id="variable_declarationdeclare-a-variable"></span>


Declaring a variable, consists of telling the [[Computer_Program|program]] to reserve a piece of memory to store some data.
<span id="c"></span>
== C# ==


<span id="declare-a-variable-in-c"></span>
In [[C_Sharp|C#]], a variable declaration follows this format:
== Declare a Variable in C# ==
 
A variable declaration in [[C_Sharp|C#]], follows the following format:


<sub>🤖 ''Pseudocode''</sub>
<sub>🤖 ''Pseudocode''</sub>


<syntaxhighlight lang="console"><DATA_TYPE> <NAME> = <VALUE>;</syntaxhighlight>
<syntaxhighlight lang="console"><DATA_TYPE> <NAME> = <VALUE>;</syntaxhighlight>
# <code>&lt;DATA_TYPE&gt;</code>: the value [[Data_Type|data type]] (number, text, etc.).
# <code>&lt;DATA_TYPE&gt;</code>: The [[Data_Type|type]] of value the [[Variable|variable]] will hold (e.g., number, text, [[Boolean|boolean]]).
# <code>&lt;NAME&gt;</code>: a recognizable name.
# <code>&lt;NAME&gt;</code>: A recognizable name for the variable.
# <code>&lt;VALUE&gt;</code>: the initial value we want to store (optional).
# <code>&lt;VALUE&gt;</code>: The initial value to store (optional; a variable can also be declared without an initial value).


<syntaxhighlight lang="csharp">// An integer variable named score with the initial value of 0.
<syntaxhighlight lang="csharp">// An integer variable named score with the initial value of 0.
Line 23: Line 21:
// A boolean variable named named isAlive with the initial value of true.
// A boolean variable named named isAlive with the initial value of true.
bool isAlive = true;</syntaxhighlight>
bool isAlive = true;</syntaxhighlight>
ℹ️ While it seems easy, [[Naming_Variables|naming variables]] is something that requires some careful thought.


-----
Once declared, you can reference a variable by its name to read or update its value:
<syntaxhighlight lang="csharp">score = score + 10;          // update
Console.WriteLine(score);    // read / display</syntaxhighlight>
<span id="python"></span>
== Python ==
In Python, variables are dynamically typed, meaning you do not need to specify a data type when declaring a variable. The type is inferred from the value assigned.
<sub>🤖 Pseudocode</sub>
<syntaxhighlight lang="console"><NAME> = <VALUE></syntaxhighlight>
<syntaxhighlight lang="python">Examples
score = 0            # integer
username = "Ze"      # string
is_alive = True      # boolean</syntaxhighlight>
-----
Once declared, you can reference a variable by its name to read or update its value:
<syntaxhighlight lang="python">score += 10          # update
print(score)          # read / display</syntaxhighlight>
<span id="javascript"></span>
== JavaScript ==
In JavaScript, you can declare variables using <code>var</code>, <code>let</code>, or <code>const</code>.
* <code>var</code>: Older syntax, [[Function|function]]-scoped.
* <code>let</code>: Modern syntax, block-scoped, used for variables that can change.
* <code>const</code>: Block-scoped, used for values that won’t change.
<sub>🤖 Pseudocode</sub>
<syntaxhighlight lang="console"><KEYWORD> <NAME> = <VALUE>;</syntaxhighlight>
<syntaxhighlight lang="javascript">Examples
let score = 0;          // variable that can change
const username = "Ze";  // constant variable
var isAlive = true;      // older syntax</syntaxhighlight>
<blockquote>ℹ️ Use let for most variables in modern code and const for values that should remain constant.
</blockquote>
-----
Once declared, you can reference a variable by its name to read or update its value:
<syntaxhighlight lang="javascript">score += 10;            // update
console.log(score);    // read/display</syntaxhighlight>
<span id="related-concepts"></span>
<span id="related-concepts"></span>
== Related Concepts ==
== Related Concepts ==
Line 31: Line 78:
* ''Uses'': [[Variable|Variable]]
* ''Uses'': [[Variable|Variable]]
* ''Mentions'': [[Naming_Variables|Naming Variables]]
* ''Mentions'': [[Naming_Variables|Naming Variables]]
* ''Performs'': [[Variable_Declaration|Variable Declaration]]

Latest revision as of 17:25, 23 October 2025

C#

In C#, a variable declaration follows this format:

🤖 Pseudocode

<DATA_TYPE> <NAME> = <VALUE>;
  1. <DATA_TYPE>: The type of value the variable will hold (e.g., number, text, boolean).
  2. <NAME>: A recognizable name for the variable.
  3. <VALUE>: The initial value to store (optional; a variable can also be declared without an initial value).
// An integer variable named score with the initial value of 0.
int score = 0;

// A string variable named username with the initial value of "Ze".
string username = "Ze";

// A boolean variable named named isAlive with the initial value of true.
bool isAlive = true;

Once declared, you can reference a variable by its name to read or update its value:

score = score + 10;          // update
Console.WriteLine(score);    // read / display

Python

In Python, variables are dynamically typed, meaning you do not need to specify a data type when declaring a variable. The type is inferred from the value assigned.

🤖 Pseudocode

<NAME> = <VALUE>
Examples
score = 0             # integer
username = "Ze"       # string
is_alive = True       # boolean

Once declared, you can reference a variable by its name to read or update its value:

score += 10           # update
print(score)          # read / display

JavaScript

In JavaScript, you can declare variables using var, let, or const.

  • var: Older syntax, function-scoped.
  • let: Modern syntax, block-scoped, used for variables that can change.
  • const: Block-scoped, used for values that won’t change.

🤖 Pseudocode

<KEYWORD> <NAME> = <VALUE>;
Examples
let score = 0;           // variable that can change
const username = "Ze";   // constant variable
var isAlive = true;      // older syntax

ℹ️ Use let for most variables in modern code and const for values that should remain constant.


Once declared, you can reference a variable by its name to read or update its value:

score += 10;            // update
console.log(score);     // read/display

Related Concepts