Data Type
- In Turkish
- Veri türü
In short
A data type is a classification that tells a program what kind of value a piece of data holds, such as a number or text, and which operations work on it.
What is a data type?
A data type describes what kind of value something is and which operations make sense for it. You can add two numbers or join two pieces of text, but multiplying a date by a sentence has no meaning. Every value in a program, whether it is stored in a variable, passed to a function, or returned from one, has a data type.
Most languages provide a small set of built-in primitive types, such as integers, floating-point numbers (numbers with decimals), strings (text), and booleans (true or false). On top of these, they offer composite types that group several values together, such as arrays, objects, and classes. In JavaScript, the typeof operator reveals a value's type while the program runs: typeof 42 returns "number", and typeof "hi" returns "string".
Data types work like the shapes of electrical plugs and sockets: a plug only fits the sockets designed for it, which stops you from connecting things that don't belong together. Languages differ in when they check types. Statically typed languages such as Java, C#, Rust, and TypeScript check types before the program runs, while dynamically typed languages such as JavaScript and Python check them while it runs.
Static versus dynamic typing is often confused with strong versus weak typing. Static and dynamic describe when types are checked, while strong and weak describe how strictly a language refuses to mix types automatically. JavaScript is dynamic and fairly weak, so "5" + 1 quietly produces "51", while Python is dynamic but strong and raises a TypeError for the same operation.
Key takeaways
- A data type defines what kind of value data holds and which operations are allowed on it.
- Common primitive types include integers, floating-point numbers, strings, and booleans.
- Composite types such as arrays and objects group several values together.
- Static typing checks types before the program runs; dynamic typing checks them while it runs.
Example
// Primitive types
const age: number = 30;
const userName: string = "Ada";
const isAdmin: boolean = false;
// A composite type built from primitives
type User = { name: string; age: number; isAdmin: boolean };
const user: User = { name: userName, age, isAdmin };
console.log(typeof age); // "number" (checked while the program runs)
console.log("5" + 1); // "51": the number is converted to text
// const total: number = "10"; // Error: Type 'string' is not assignable to type 'number'Readers ask
What are primitive data types?
Primitive data types are the simplest built-in types a language provides, such as numbers, strings, and booleans, which are not made up of other values. JavaScript has seven: string, number, bigint, boolean, undefined, symbol, and null.
What is the difference between static and dynamic typing?
In a statically typed language, types are checked before the program runs, usually by a compiler, so many mistakes are caught early. In a dynamically typed language, types are checked while the program runs, which is more flexible but lets type errors slip through until that code actually executes.
Why is 0.1 + 0.2 not equal to 0.3?
Most languages store decimal numbers as binary floating-point values, which cannot represent some fractions, like 0.1, exactly. The tiny rounding errors add up, so 0.1 + 0.2 gives 0.30000000000000004; for money, use whole numbers such as cents or a dedicated decimal type.
See also
- VariableProgramming Fundamentals, p. 56A variable is a named storage location in a program that holds a value, such as a number or a piece of text, which the code can read and change as it runs.
- ArrayProgramming Fundamentals, p. 3An array is an ordered collection of values stored under one name, where each item is accessed by its numeric position, called an index, usually starting at 0.
- ClassProgramming Fundamentals, p. 9A class is a blueprint in object-oriented programming that defines the data and behavior shared by a group of objects, which are created from it as instances.
- TypeScriptWeb Development, p. 50TypeScript is a programming language built on JavaScript that adds static types, catching many bugs before the code runs, and compiles to plain JavaScript.
- CompilerProgramming Fundamentals, p. 11A compiler is a program that translates source code written in a programming language into a lower-level form, such as machine code, that a computer can run.
- Static TypingProgramming Fundamentals, p. 53Static typing means the types of variables and expressions are checked before the program runs, usually by the compiler, so many type errors are caught early.
- Dynamic TypingProgramming Fundamentals, p. 16Dynamic typing means types belong to values, not variables, and are checked while the program runs, so a variable can hold a number now and a string later.
- EnumProgramming Fundamentals, p. 18An enum is a data type that defines a fixed set of named values, such as an order's possible statuses, so code can't use a value outside that set.
Spotted a mistake or something missing on this page?Suggest an edit