@@ -249,22 +249,61 @@ Are functions objects?
249249/*
250250What is the difference between value(primitive) types and reference types?
251251////////////////////////////////////////////////////////////////////////////
252- • VUnderstanding value and refrence types is important for understanding prototypes.
252+ • Understanding value and refrence types is important for understanding prototypes.
253+
253254 • Value types (primitives) are: numbers, strings, booleans, symbols, undefined, null.
254- • value types are stored in the variable.
255- • primitives are copied by thier value.
256- • Reference types are: objects, functions, arrays.
257- • primtives are independent
255+ • primtives are independent
256+ • primtives are COPIED BY VALUE
257+ • primitive value types are stored in the variable. */
258+
259+
260+ let x = 10 ; // the value of 10 is stored inside x
261+ let y = x ; // when x is copied into y, it is copied exactly as 10 into the new variable... it is indpendant now.
262+
263+ x = 20 ; // so if you change the value of x to 20...
264+
265+ console . log ( x ) // 20 is the the value of x because we changed it...
266+ console . log ( y ) // 10 is the value of y because when we copied it it was 10 and it is independant.
267+
268+ /* • Reference types are: objects, functions, arrays.
269+ • Reference types are copied by thier reference (i.e. thier value sotred somewhere in memory)
270+ • When Reference types are used, the program is pointed to the address location of where it is stored.
258271 • reference types are NOT stored in the variable... it is stored somewhere else in memory
259272 • objects are copied by thier reference.
260273*/
261- let x = 10 ; // the value of 10 is stored inside x
262- let y = x ; // when x is copied into y, it is copied exactly as 10 into the new variable... it is indpendant now.
274+ let x = { value : 10 } ; // 1. since an object is not stored in the variable, but somewhere else in memory.
275+ let y = x ; // 3. So when x is copied into y, it is the ADDRESS that is copied, which will have the updated number.
276+
277+ x . value = 20 ; // 2. when you change a value in that object, the value in that address is changed,
278+
279+ console . log ( x ) // 20 Both x and y are pointed to the same address, the same object in memory.
280+ console . log ( y ) // 20
263281
264- x = 20 ; // so if you change the value of x to 20...
282+ /* Additional example: */
283+
284+ let number = 10 ;
285+
286+ function increment ( number ) {
287+ number ++ ;
288+ }
289+
290+ increment ( number ) ;
291+ console . log ( number ) ; // 10... because primitives are copied by value, and when you copy number into increment it will
292+ // be independent... which is still 10.
293+
294+ // However, when dealing with reference types:
295+
296+ let number = { value : 10 } ;
297+
298+ function increment ( number ) {
299+ number . value ++ ;
300+ }
301+
302+ increment ( number ) ;
303+ console . log ( number ) ; // 11... because the number is stored elsewhere in memory and passed by reference. In this case there is NOT
304+ // two independent copies but one address
305+
265306
266- console . log ( x ) // 20 is the
267- console . log ( y ) // 10
268307
269308
270309/*
0 commit comments