Dark mode
Variables (ตัวแปร)
Variables are named storage locations in memory that are used to store values during program execution. Variables allow us to reference and manipulate data efficiently.
Variable Declaration and Assignment
js
// Variable declaration in JavaScript
let name = "John"; // variable that can be changed
const age = 25; // constant variable that cannot be changed
var score = 85; // old variable declaration (not recommended in modern code)
// Changing variable values
name = "Jane";
console.log(name); // Jane
// Cannot change constant variables
// age = 26; // will cause TypeError: Assignment to constant variable
rust
// Variable declaration in Rust
fn main() {
let name = "John"; // immutable variable by default
let mut age = 25; // mutable variable that can be changed
const SCORE: i32 = 85; // constants must specify data type
// Changing variable values
// name = "Jane"; // will cause error[E0384]: cannot assign twice to immutable variable
age = 26; // can be changed because declared with mut
println!("Age: {}", age); // Age: 26
}
python
# Variable declaration in Python
name = "John" # variable that can be changed
age = 25 # all variables are mutable in Python
SCORE = 85 # constants by convention use uppercase (but can still be changed)
# Changing variable values
name = "Jane"
print(name) # Jane
# Even "constants" can be changed in Python
SCORE = 90 # This works but is discouraged by convention
print(SCORE) # 90
go
// Variable declaration in Go
package main
import "fmt"
func main() {
// Explicit declaration with type specification
var name string = "John"
// Short declaration with type inference
age := 25
// Constant declaration
const score = 85
// Changing variable values
name = "Jane"
age = 26
fmt.Println(name) // Jane
fmt.Println(age) // 26
// Cannot change constant values
// score = 90 // will cause: cannot assign to score (constant)
}
Variable Scope (ขอบเขตของตัวแปร)
Variable scope defines the regions in code where a variable can be accessed and used.
js
// Variable scope in JavaScript
// Global scope - variables accessible from all parts of the code
const globalVar = "I am a global variable";
function showScope() {
// Function scope - variables accessible only within the function
const localVar = "I am a local variable";
console.log(globalVar); // can access global variables
console.log(localVar); // can access local variables
if (true) {
// Block scope - variables accessible only within the block (let, const)
const blockVar = "I am a block variable";
console.log(blockVar); // can only be accessed within this block
}
// console.log(blockVar); // Error: blockVar is not defined - cannot access outside the block
}
showScope();
console.log(globalVar); // can access global variables from anywhere
// console.log(localVar); // Error: localVar is not defined - cannot access local variables from outside the function
rust
// Variable scope in Rust
fn main() {
// main function scope
let global_var = "I am a variable in main scope";
fn show_scope() {
// show_scope function scope
let local_var = "I am a variable in show_scope function";
// println!("{}", global_var); // Error: cannot access variables from outer scope
println!("{}", local_var);
}
show_scope();
// Block scope
{
let block_var = "I am a variable in a block";
println!("{}", block_var);
println!("{}", global_var); // can access variables from outer scope
}
// println!("{}", block_var); // Error: cannot find value `block_var` in this scope
}
python
# Variable scope in Python
# Global scope - variables accessible from all parts of the module
global_var = "I am a global variable"
def show_scope():
# Function scope - variables accessible only within the function
local_var = "I am a local variable"
print(global_var) # can access global variables
print(local_var) # can access local variables
if True:
# Python doesn't have block scope - variables declared in blocks
# are accessible in the entire function
block_var = "I am a variable in a block"
print(block_var)
print(block_var) # can access "block" variables outside the block
show_scope()
print(global_var) # can access global variables from anywhere
# print(local_var) # NameError: name 'local_var' is not defined - cannot access outside the function
# To modify global variables inside functions, use the global keyword
def modify_global():
global global_var
global_var = "Modified global variable"
modify_global()
print(global_var) # "Modified global variable"
go
// Variable scope in Go
package main
import "fmt"
// Package-level variables - accessible from all functions in the same package
var packageVar = "I am a package-level variable"
func main() {
// Function variables - accessible only within the main function
funcVar := "I am a variable in main function"
fmt.Println(packageVar) // can access package-level variables
fmt.Println(funcVar) // can access function variables
// Block scope
{
blockVar := "I am a variable in a block"
fmt.Println(blockVar) // can access block variables
fmt.Println(funcVar) // can access variables from outer scope
}
// fmt.Println(blockVar) // Error: undefined: blockVar - cannot access outside the block
showScope()
}
func showScope() {
fmt.Println(packageVar) // can access package-level variables
// fmt.Println(funcVar) // Error: undefined: funcVar - cannot access variables from other functions
}