Skip to main content

Variables and Data Types

Table of Contents

No.Topic
1Variables
2Data Types

Definition of Variables

A variable is like a container used to store data such as numbers, text, or other values in a program. It allows you to use and change that data later by referring to the variable’s name.


C++

Syntax

<datatype> <variableName> = <value>;

Common data types: int, string (from <string>), double, bool, char

Note: To use string, include the <string> header and use the std namespace.


Example

#include <iostream>
#include <string> // Required for using std::string

int main() {
int age = 25;
std::string name = "Alice";
bool isStudent = true;

std::cout << name << " is " << age << " years old." << std::endl;
return 0;
}

C#

Syntax

<datatype> <variableName> = <value>;

Common data types: int, string, double, bool

Example

int age = 25;
string name = "Alice";
bool isStudent = true;

Console.WriteLine(name + " is " + age + " years old.");

Java

Syntax

<datatype> <variableName> = <value>;

Common data types: int, String, double, boolean

Example

int age = 25;
String name = "Alice";
boolean isStudent = true;

System.out.println(name + " is " + age + " years old.");

Python

Syntax

variable_name = value
  • No need to declare type — Python figures it out automatically

Example

age = 25
name = "Alice"
is_student = True

print(name + " is " + str(age) + " years old.")

JavaScript

Syntax

let variableName = value;
const variableName = value; // for constants
var variableName = value; // older way

Example

let age = 25;
let name = "Alice";
let isStudent = true;

console.log(name + " is " + age + " years old.");

TypeScript

Syntax

In TypeScript, variables can be declared using let, const, or var with type annotations.

let variableName: type = value;
const constantName: type = value;
var oldStyleVariable: type = value;

Example

let age: number = 25;
let name: string = "Alice";
let isStudent: boolean = true;

console.log(`${name} is ${age} years old.`);


Back to Top

Definition of Data Types

Data types define what kind of value a variable can store, such as numbers, text, true/false, etc. Each programming language has built-in data types used to store specific kinds of data.


C++

Common Data Types:

  • int – whole numbers (e.g., 10)
  • double – decimal numbers (e.g., 10.5)
  • std::string – text (e.g., "Hello") (requires <string> header)
  • bool – true/false values
  • char – single character (e.g., 'A')

Example

#include <iostream>
#include <string> // Required for std::string

int main() {
int age = 30;
double height = 5.9;
std::string name = "Alice";
bool isStudent = true;
char grade = 'A';

std::cout << name << " is " << age << " years old, height is " << height << " feet." << std::endl;
std::cout << "Student status: " << (isStudent ? "Yes" : "No") << ", Grade: " << grade << std::endl;

return 0;
}

C#

Common Data Types:

  • int – whole numbers (e.g., 10)
  • double – decimal numbers (e.g., 10.5)
  • string – text (e.g., "Hello")
  • bool – true/false values
  • char – single character (e.g., 'A')

Example

int age = 30;
double height = 5.9;
string name = "Alice";
bool isStudent = true;
char grade = 'A';

Java

Common Data Types:

  • int – whole numbers
  • double – decimal numbers
  • String – text
  • boolean – true/false
  • char – single character

Example

int age = 30;
double height = 5.9;
String name = "Alice";
boolean isStudent = true;
char grade = 'A';

Python

Common Data Types:

  • int – whole numbers
  • float – decimal numbers
  • str – text
  • bool – true/false (True or False)
  • list, dict, tuple – for collections

Python is dynamically typed — no need to declare type.

Example

age = 30
height = 5.9
name = "Alice"
is_student = True
grade = 'A'

JavaScript

Common Data Types:

  • Number – for all numbers (both integer and decimal)
  • String – text
  • Boolean – true/false
  • Object – for key-value pairs
  • Array – list of items
  • null – empty value
  • undefined – not assigned

Example

let age = 30;
let height = 5.9;
let name = "Alice";
let isStudent = true;
let grade = 'A';

TypeScript

Common Data Types:

TypeScript builds on JavaScript by adding static type definitions. Below are the most commonly used data types in TypeScript:

TypeExampleDescription
string"Hello, World!"Textual data
number42, 3.14Both integers and floating-point numbers
booleantrue, falseLogical values
nullnullAbsence of any value
undefinedundefinedA variable not assigned a value
anyanyDisables type checking
voidfunction greet(): void {}Function that does not return a value
neverfunction error(): never {}Represents unreachable code or errors
object{ name: "Alice", age: 30 }Non-primitive values
arraystring[], Array<number>Ordered collections
tuple[string, number]Fixed-length array with known types
enumenum Color {Red, Green, Blue}Named constants
union`stringnumber`
literal"yes", 42Specific value as type
type / interfacetype User = {name: string}Custom structured types

Tip: Use type and interface to define reusable and complex data structures.


Type Safety is the key feature of TypeScript—leverage these data types to write robust code.

Example

let age: number = 30;
let height: number = 5.9;
let name: string = "Alice";
let isStudent: boolean = true;
let grade: string = 'A'; // TypeScript does not have a 'char' type, use string with 1 character


Back to Top