• Variables

    In programming, variables are used to store data values. These values can be of different data types, such as strings, integers, floating-point numbers, boolean, arrays, or objects. Variables are assigned names so that they can be referenced in the code. They can be any size and shape, and can hold anything from numbers to strings of text. You can use variables to keep track of things like user input, or the result of a calculation. The name of a variable can be anything you want, as long as it doesn’t start with a number and isn’t a reserved word in the language. Once a variable is given a value, that value can be changed throughout the program by reassigning the variable. Variables are one of the most important tools in programming, and understanding how to use them is essential to becoming a good programmer.

      • String Variables

      • A string variable is simply a variable that can hold multiple pieces of text (a word, phrase, or sentence). However, sometimes we need to store more than just text. We might want to store a person's name, which can be multiple words. In this case, we need to use a string variable.

      • Javascript

        Example of string variable in javascript.
        var name = 'John';
        
        console.log(name); // John
      • Dart

        Example of string variable in dart.
        void main() {
          var name = 'John';
        
          print(name); // John
        }
        
      • D

        Example of string variable in d.
        import std.stdio : write;
        import std.string;
        
        void main()
        {
        	string name = "John";
        
        	write(name); // John
        }
        
      • Php

        Example of string variable in php.
        <?php
        $name = 'John';
        
        print($name); // John
      • Python

        Example of string variable in python.
        name = 'John'
        
        print(name) # John
      • Go

        Example of string variable in go.
        package main
        
        import "fmt"
        
        func main() {
        	var name string = "John"
        
        	fmt.Println(name) // John
        }
        
      • Integer Variables

      • An integer variable is a data type in computer programming that stores numerical values. Integer variables can be used to represent whole numbers, such as 1, 2, 3, 4, 5, etc. They can also be used to represent negative numbers, such as -1, -2, -3, -4, -5, etc. Integer variables are commonly used in programs to store counts or loop indices.

      • Javascript

        Example of integer variable in javascript.
        var age = 30;
        
        console.log(age); // 30
      • Dart

        Example of integer variable in dart.
        void main() {
          var age = 30;
        
          print(age); // 30
        }
      • D

        Example of integer variable in d.
        import std.stdio : write;
        
        void main()
        {
        	int age = 30;
        
        	write(age); // 30
        }
        
      • Php

        Example of integer variable in php.
        <?php
        $age = 30;
        
        print($age); // 30
      • Python

        Example of integer variable in python.
        age = 30
        
        print(age) # 30
      • Go

        Example of integer variable in go.
        package main
        
        import "fmt"
        
        func main() {
        	var age int = 30
        
        	fmt.Println(age) // 30
        }
        
      • Float Variables

      • Float variables, like integer variables, hold numerical values but are presented in decimal form. It is possible to represent decimal numbers such as 1.01, 2.20, 3.33, 4.44, 5.555, etc. in float variables. They can also be used to represent negative decimal numbers, such as -1.01, -2.20, -3.33, -4.44, -5.555, etc.

      • Javascript

        Example of float variable in javascript.
        var pi = 3.14;
        
        console.log(pi); // 3.14
      • Dart

        Example of float variable in dart.
        void main() {
          var pi = 3.14;
        
          print(pi); // 3.14
        }
        
      • D

        Example of float variable in d.
        import std.stdio : write;
        
        void main()
        {
        	float pi = 3.14;
        
        	write(pi); // 3.14
        }
        
      • Php

        Example of float variable in php.
        <?php
        $pi = 3.14;
        
        print($pi); // 3.14
      • Python

        Example of float variable in python.
        pi = 3.14
        
        print(pi)  # 3.14
        
      • Go

        Example of float variable in go.
        package main
        
        import "fmt"
        
        func main() {
        	var pi float32 = 3.14
        
        	fmt.Println(pi) // 3.14
        }
        
      • Boolean Variables

      • A boolean variable is a type of data that can only have one of two possible values, either true or false. In programming, boolean variables are often used to track whether a certain condition has been met, or to control the flow of a program. For example, a program might use a Boolean variable to keep track of whether a user is logged in or not.

      • Javascript

        Example of boolean variable in javascript.
        var isTruthy = true;
        var isFalsy = false;
        
        console.log(isTruthy); // true
        console.log(isFalsy); // false
      • Dart

        Example of boolean variable in dart.
        void main() {
          var isTruthy = true;
          var isFalsy = false;
        
          print(isTruthy); // true
          print(isFalsy); // false
        }
      • D

        Example of boolean variable in d.
        import std.stdio : writeln;
        
        void main()
        {
        	bool isTruthy = true;
        	bool isFalsy = false;
        
        	writeln(isTruthy); // true
        	writeln(isFalsy); // false
        }
        
      • Php

        Example of boolean variable in php.
        <?php
        $is_truthy = true;
        $is_falsy = false;
        
        print($is_truthy); // true
        print($is_truthy); // false
      • Python

        Example of boolean variable in python.
        isTruthy = True
        isFalsy = False
        
        print(isTruthy) # True
        print(isFalsy) # False
      • Go

        Example of boolean variable in go.
        package main
        
        import "fmt"
        
        func main() {
        	var isTruthy bool = true
        	var isFalsy bool = false
        
        	fmt.Println(isTruthy) // true
        	fmt.Println(isFalsy)  // false
        }
        
      • Array Variables

      • Array variables are used in many programming languages to store data sets of various sizes. An array is a type of data structure that stores a collection of elements. Each element in the array has a specific index, which can be used to access it.

      • Javascript

        Example of array variable in javascript.
        var fruits = ['apple', 'banana', 'cherry'];
        
        console.log(fruits[1]); // banana
      • Dart

        Example of array variable in dart.
        void main() {
          var fruits = ['apple', 'banana', 'cherry'];
        
          print(fruits[1]); // banana
        }
      • D

        Example of array variable in d.
        import std.stdio : write;
        import std.string;
        import std.array;
        
        void main()
        {
        	string[] fruits = ["apple", "banana", "cherry"];
        
        	write(fruits[1]); // banana
        }
        
      • Php

        Example of array variable in php.
        <?php
        $fruits = array('apple', 'banana', 'cherry');
        
        print($fruits[1]); // banana
      • Python

        Example of array variable in python.
        fruits = ['apple', 'banana', 'cherry']
        
        print(fruits[1]) # banana
      • Go

        Example of array variable in go.
        package main
        
        import "fmt"
        
        func main() {
        	var fruits = [3]string{"apple", "banana", "cherry"}
        
        	fmt.Println(fruits[1]) // banana
        }
        
      • Object Variables

      • An object variable is a type of variable that is used to reference an object. The value of an object variable is an address that identifies the location of the object in memory. When an object is assigned to an object variable, a new reference to the object is created. An object is a type of data structure that holds a collection of properties and methods. For each property and method, a name is used to access them.

      • Javascript

        Example of object variable in javascript.
        class Person {
        	constructor(name, age) {
        		this.name = name;
        		this.age = age;
        	}
        
        	greet() {
        		console.log('Hello my name is ' + this.name);
        	}
        }
        
        var human = new Person('John', 36);
        
        console.log(human.name); // John
        console.log(human.age); // 36
        human.greet(); // Hello my name is John
      • Dart

        Example of object variable in dart.
        class Person {
          String name;
          int age;
        
          Person(this.name, this.age);
        
          void greet() {
        	print('Hello my name is ' + this.name);
          }
        }
        
        void main() {
          var human = new Person('John', 36);
        
          print(human.name); // John
          print(human.age); // 36
          human.greet(); // Hello my name is John
        }
      • D

        Example of object variable in d.
        import std.stdio : write, writeln;
        import std.string;
        
        class Person {
        	string name;
        	int age;
        
        	this(string name, int age) {
        		this.name = name;
        		this.age = age;
        	}
        
        	void greet() {
        		write("Hello my name is ", this.name);
        	}
        }
        
        void main()
        {
        	Person human = new Person("John", 36);
        
        	writeln(human.name); // John
        	writeln(human.age); // 36
        	human.greet(); // Hello my name is John
        }
      • Php

        Example of object variable in php.
        <?php
        class Person {
        	public $name, $age;
        
        	function __construct($name, $age) {
        		$this->name = $name;
        		$this->age = $age;
        	}
        
        	public function greet() {
        		print('Hello my name is ' . $this->name);
        	}
        }
        
        $human = new Person('John', 36);
        
        print($human->name); // John
        print($human->age); // 36
        $human->greet(); // Hello my name is John
      • Python

        Example of object variable in python.
        class Person:
        	def __init__(self, name, age):
        		self.name = name
        		self.age = age
        
        	def greet(self):
        		print('Hello my name is ' + self.name)
        
        
        human = Person('John', 36)
        
        print(human.name)  # John
        print(human.age)  # 36
        human.greet()  # Hello my name is John
        
      • Go

        Example of object variable in go.
        package main
        
        import "fmt"
        
        type Person struct {
        	name string
        	age  int
        }
        
        func (p Person) greet() {
        	fmt.Println("Hello my name is " + p.name)
        }
        
        func main() {
        	var human = Person{"John", 36}
        
        	fmt.Println(human.name) // John
        	fmt.Println(human.age)  // 36
        	human.greet()		   // Hello my name is John
        }