Coder Social home page Coder Social logo

reference-vs-value's Introduction

Storing by Reference vs. Value

  • "Object" is a confusing word in JS. It can have two different meanings:
    1. Anything that isn't a primitive (arrays, functions, objects. for now). This is because of how JavaScript handles inheritance and types behind the scenes. Take our word for it right now, you'll learn how it works later on.
    2. The data structure defined by curly braces: {property: "value"};
  • Primitives vs Objects
  • Storing by value:
    • Variables that are assigned primitive values directly store a copy of that value.
    • Modifying a variable's primitive value does not effect memory anywhere else in the program.
  • Storing by reference:
    • Variables that are assigned object values (functions, arrays, objects) do not store their own copy of that object. They store a pointer to where that object lives in memory.
    • Modifying a variable that points to an object will immediately effect any other variable pointing to that same object.
  • Side Effects: When making a change to one piece of your program causes changes somewhere else. We see this with the two variables reference_1 and reference_2.
  • Garbage collecting. If no variables are pointing to an object in memory, that object is un-reachable and will be deleted from memory.

The Code

let value_1 = 5;
let reference_1 = [];

let value_2 = value_1;
let reference_2 = reference_1;

value_2++;
reference_2.push(value_2);
console.log(reference_1);

reference_1 = null;
reference_2 = null;

PythonTutor link


The Sketches




reference-vs-value's People

Contributors

colevanderswands avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.