What is the definition of Data Type?

What is the definition of Data Type?
0.0 0

#1

What is the definitionn of Data Type? I know what it is, string, boolean, number, etc.

“A value that has an effect on code” ?


#2

I look at it this way.

All data is stored on the computer as 1s and 0s. All data in memory is indistinguishable. If you just called up a bunch of data in binary and tried to figure out what it was, you’d be hard pressed to figure it out. If you looked at a byte that was 01000001, how would you know what that is? Is it an 8-bit integer 65? Is it an 8-bit letter code for the letter “A”, perhaps part of a larger string? Is it part of a larger 16-bit integer or a floating point? Or some 32-bit data? Or is it just user encoded bitwise data? There are many other possibilities.

The program needs to keep track of what data is where and what type of data it is. As languages go, JS is kind of light and loose with data types - it doesn’t have many and it doesn’t much care what you do with them. But there are important distinctions and when JS automatically converts between them, you need to know what it is doing.

Of the top of my head, if I had to define “data type” it would be something like, “A classification for how computer memory is to be used and interpreted.” It is the key that helps the program/OS interpret the binary data as it was intended to be used.


#3

The most basic answer is that data types are the way we instruct a program to interpret binary data. Your computer processes and holds all data as (probably) 64-bit words. A simple example:

00000000 00000000 00000000 00000000 00000000 00000000 00000000 01100111

By itself, this is a meaning string of 0’s and 1’s, but if we tell the program that it’s an ASCII character, then it becomes “g”. If we tell the program that it’s an integer, it becomes 103. At lower levels, the CPU may interpret this as a memory address, a set of instructions, or just some data. We call these “primitive types”.

Types like arrays, strings, classes, and objects are different. These aren’t just made up of a single word in memory, but are defined by a set of behaviors that make them useful. For instance, an array can have 1,000,000 elements or just 1 (or even none), but what makes it an array is how it’s stored and accessed. All languages allow you to use arrays is more-or-less the same way. Strings are arrays of characters. Classes, objects, and other types you may not have used like dictionaries, hash maps, and lists have their own specifications. These are called “abstract data types” because we can talk about how they should behave in any language. Think of these as tiny programs that the language uses to organize data.


#4

Another definition of “data type” is (more or less): a collection (or group) of values associated with operations you could do with them or apply to them. Like integers and + - * / etc. or booleans and logical operations, or strings and concatenation etc. Also you can distinguish between primitive data types (int, bool, char, float, etc.) and more complex, compound data types like strings, linked lists, heaps, tries, etc.


#5

There are also higher order data types ( as opposed to primitives)

These are
Classes ( also known as user-defined data types)
Interface
Enum
List


#6

I’d like to expand on this one for JavaScript specifically:

Primary (primitive) data types:

  • String
  • Number (also NaN, Infinity, -Infinity, +/- 0)
  • Boolean

Composite (compound) data types:

  • Object
  • Array

Special data types:

  • null
  • undefined

Semantically, data types enforce:

  1. The kinds of values they can hold (3 vs "3" vs [3]).
  2. The operations that can be applied to them in behavior and result. (1+1==2 vs "1"+"1"=="11")

Also important is passing by value or reference (like as arguments to a function). “Values” of Primitive data types are copied (by value), whereas “instances” of Composite data types like Object and Array are passed their pointers (by reference). Below, for the arrays and objects, a new instance is created and a pointer assigned each time the interpreter evaluates the [] or the {}. The pointer is an address (itself a number) to where the group of values are in memory. So when the equality == operator is applied to an object or array, the interpreter is comparing their addresses.

1 == 1     // true
"1" == "1" // true
[] == []   // false
{} == {}   // false
var a = []
a == a     // true

#7

This is commonly stated, but not actually accurate. JavaScript, along with Java, Ruby, Python, and probably some other languages, uses “pass by sharing” for all values, primitives or otherwise. All variables in JavaScript are pointers, with arrays and objects being pointers to pointers. The difference between pass by reference and pass by sharing can be seen anytime you reassign a function parameter that was passed an object reference:

var myObj = { name: "Thing" };

function changeTheThing (param) {
   param = { name: "A totally different thing" };
}

changeTheThing(myObj); // passing object reference to function

console.log(myObj); // "{ name: "Thing" }" - it doesn't change

// If we passed objects by reference, we would see

console.log(myObj); // "{ name: "A totally different thing" }"

// because it reassigns the original object

Otherwise, good and important info.


#8

Thanks for the correction, I missed the nuance of that before.

Found that assigning the name property a value, rather than assigning a new object to the param variable, does seem to mutate myObj.

function changeTheThing(param) {
    param.name = "A totally different thing";
}

Why is this?


#9

Well, you weren’t wrong that the function is passed a pointer value. When you use the assignment operator on an object, you’re accessing a pointer referenced from param's own pointer value. As long as param points to the same object, you’ll be able to mutate it. Pass-by-sharing is really just pass-by-reference with an extra reference, so you can expect the same effects unless you break that link between the variable and its value. You can also mutate arrays in a function for the same reason.