Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value and reference types

Tags:

I know that there are 6 data types in JavaScript.

What are the "reference" types in JavaScript and what are the "value" data types in JavaScript?. Could someone list them by these 2 categories?

like image 820
obenjiro Avatar asked Jul 17 '12 11:07

obenjiro


People also ask

What is difference between reference type and value type?

There are two kinds of types in Visual Basic: reference types and value types. Variables of reference types store references to their data (objects), while variables of value types directly contain their data.

What is value type and reference type in Swift?

In Swift there are two categories of types: value types and reference types. A value type instance keeps a unique copy of its data, for example, a struct or an enum . A reference type, shares a single copy of its data, and the type is usually a class .

What is reference type data type?

Reference datatypes in java are those which contains reference/address of dynamically created objects. These are not predefined like primitive data types. Following are the reference types in Java. class types − This reference type points to an object of a class. array types − This reference type points to an array.

Is classified into value types and reference types?

In C#, these data types are categorized based on how they store their value in the memory. C# includes the following categories of data types: Value type.


2 Answers

undefined, null, number, string, boolean and object of which only object is a "reference" type.

There is no assignment by reference or pass by reference in javascript, whenever you pass/assign a "reference" type, you pass/assign a copy of the reference, you don't create a reference of the reference which would have different implications.

You can use these functions:

function isReferenceType( value ) {
     return Object(value) === value;
}

function isPrimitiveType( value ) {
     return Object(value) !== value;
}
like image 70
Esailija Avatar answered Sep 19 '22 20:09

Esailija


From the standard#sec-8

The ECMAScript language types are Undefined, Null, Boolean, String, Number, and Object

The only "reference" type is the Object.

like image 21
Engineer Avatar answered Sep 21 '22 20:09

Engineer