Variables compared across 16 programming languages

Store values and declare or infer types – the foundation of every program.

Python JavaScript TypeScript Java C C++ C# Go Rust PHP Ruby Swift Kotlin SQL Bash HTML & CSS

PyPython

name = "Ada"
alter = 36
groesse = 1.68
aktiv = True
print(name, alter, groesse, aktiv)
print(type(alter), type(groesse))

Types are detected automatically; type() displays them.

JSJavaScript

const name = "Ada";
let alter = 36;
alter += 1;
const groesse = 1.68;
const aktiv = true;
console.log(name, alter, groesse, aktiv);
console.log(typeof alter, typeof groesse, typeof aktiv);

const cannot be reassigned, let can. Integers and decimals are both number; typeof shows the type.

TSTypeScript

const name: string = "Ada";
let alter: number = 36;
alter += 1;
const groesse = 1.68;          // type is inferred as number
const aktiv: boolean = true;
console.log(name, alter, groesse, aktiv);
console.log(typeof alter, typeof groesse, typeof aktiv);
// alter = "alt";              // compiler error

The types exist only in the source code – typeof returns JavaScript type names at runtime. With an initial value the type can usually be omitted.

JvJava

public class Main {
    public static void main(String[] args) {
        String name = "Ada";
        int alter = 36;
        double groesse = 1.68;
        boolean aktiv = true;
        var stadt = "London";   // type String is inferred
        System.out.println(name + " " + alter + " " + groesse + " " + aktiv);
        System.out.println(stadt.toUpperCase());
    }
}

Types are fixed. var only saves typing: the compiler infers the type, stadt is a String. Output: Ada 36 1.68 true and LONDON.

CC

#include <stdio.h>
#include <stdbool.h>

int main(void) {
    const char *name = "Ada";
    int alter = 36;
    double groesse = 1.68;
    bool aktiv = true;
    printf("%s %d %.2f %d\n", name, alter, groesse, aktiv);
    printf("%zu %zu\n", sizeof(alter), sizeof(groesse));
    return 0;
}

Every variable has a fixed type. The format string determines the output (%s string, %d int, %f double). sizeof returns bytes (typically 4 and 8).

C++C++

#include <iostream>
#include <string>

int main() {
    std::string name = "Ada";
    int alter = 36;
    double groesse = 1.68;
    bool aktiv = true;
    auto n = 42;   // type is deduced: int
    std::cout << name << " " << alter << " " << groesse << " " << aktiv << " " << n << '\n';
}

C++ is statically typed; auto deduces the type from the value. A bool is printed as 1 or 0 (as true/false with std::boolalpha).

C#C#

string name = "Ada";
int alter = 36;
double groesse = 1.68;
bool aktiv = true;
Console.WriteLine($"{name} {alter} {aktiv}");
Console.WriteLine($"{alter.GetType().Name} {groesse.GetType().Name}");

Types come before the name (or var for type inference); $"..." interpolates values into text.

GoGo

package main

import "fmt"

func main() {
	name := "Ada"
	alter := 36
	groesse := 1.68
	aktiv := true
	fmt.Println(name, alter, groesse, aktiv)
	fmt.Printf("%T %T\n", alter, groesse)
}

:= declares and initializes with type inference (output: Ada 36 1.68 true, then int float64). %T in Printf shows the type.

RsRust

fn main() {
    let name = "Ada";
    let mut alter: u32 = 36;
    let groesse = 1.68;
    let aktiv = true;
    alter += 1;
    println!("{name} {alter} {groesse} {aktiv}");
    let x = 5;
    let x = x * 2;          // shadowing
    println!("{x}");
}

let is immutable, mut allows changes. The type is inferred (&str, f64, bool); with shadowing you can rebind a name.

PHPPHP

<?php
$name = "Ada";
$alter = 36;
$groesse = 1.68;
$aktiv = true;
echo "$name, $alter, $groesse, " . var_export($aktiv, true) . "\n";
echo gettype($alter), " ", gettype($groesse), "\n";

Variables start with $. gettype returns the type (integer, double for float). var_export prints true/false readably, because echo true only prints 1.

RbRuby

name = "Ada"
alter = 36
groesse = 1.68
aktiv = true
puts "#{name}, #{alter}, #{groesse}, #{aktiv}"
puts alter.class, groesse.class

Ruby detects the type itself. #{...} inserts values into double-quoted strings. .class returns Integer or Float.

SwSwift

let name = "Ada"
var alter = 36
alter += 1
let groesse = 1.68
let aktiv = true
print(name, alter, groesse, aktiv)
print(type(of: alter), type(of: groesse))

let is constant, var is mutable. Types are inferred; type(of:) displays them.

KtKotlin

fun main() {
    val name = "Ada"        // val: immutable
    var alter = 36          // var: mutable
    val groesse = 1.68
    val aktiv = true
    alter += 1
    println("$name $alter $groesse $aktiv")
    println(alter::class.simpleName)
}

Types are inferred; $name inserts variables into text. Output: Ada 37 1.68 true and Int.

SQLSQL

WITH konstanten AS (
  SELECT 0.19 AS mwst, 'EUR' AS waehrung
),
ware AS (
  SELECT 'Laptop' AS name, 999.00 AS netto
)
SELECT name,
       netto,
       ROUND(netto * (1 + mwst), 2) AS brutto,
       CAST(netto AS INTEGER)       AS netto_ganzzahl,
       waehrung
FROM ware, konstanten;

SQL has no variables like programming languages; every column has a fixed type (INTEGER, TEXT, NUMERIC …). Values you need several times go into a CTE and are used via a cross product. CAST converts the type. Procedural variables exist only in dialect extensions (PL/pgSQL, MySQL SET @x).

shBash

name="Ada"
alter=36
readonly PI=3.14
echo "Name: $name, Age: $alter"
echo "Next year: $((alter + 1))"
echo "No value: ${unbekannt:-Standard}"
echo "Length of the name: ${#name}"

Assign without spaces around the =. Everything is text; $(( )) calculates with integers, ${var:-x} returns a default value and readonly protects constants.

</>HTML & CSS

<!DOCTYPE html>
<style>
:root { --primaer: #e34f26; --abstand: 1rem; --radius: 10px; }
body { font-family: system-ui, sans-serif; margin: 12px; background: #fff; color: #222; }
.karte { padding: var(--abstand); border: 2px solid var(--primaer); border-radius: var(--radius); }
.karte.gruen { --primaer: seagreen; }
h2 { color: var(--primaer); margin-top: 0; }
</style>
<div class="karte"><h2>Default</h2>Uses <code>--primaer</code></div>
<br>
<div class="karte gruen"><h2>Overridden</h2>Locally <code>--primaer: seagreen</code></div>

CSS has no classic variables, but custom properties: --name defines, var(--name) uses. They are inherited and can be overridden in subtrees – the basis for themes.

More comparisons

Hello World Conditionals Loops Functions Lists and Arrays Dictionaries and Maps Classes and Objects Error Handling Reading Files