JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 2nd, 2013, 09:04 AM
littlebigman littlebigman is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2010
Posts: 28 littlebigman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 41 m 2 sec
Reputation Power: 0
Question Primitive values immutable or not?

Hello

I'm reading David Flanagan's "javascript: The Definitive Guide 6th ed", which says on page 44:
Quote:
There is a fundamental difference in JavaScript between primitive values (undefined, null, booleans, numbers, and strings) and objects (including arrays and functions). Primitives are immutable: there is no way to change (or “mutate”) a primitive value.


And still, the following code works:
Code:
"use strict";

var myvar = "hello, world!";
myvar = "changed!";

function myfunc(){
	console.log("Before");
	//alert(myvar);
	document.write(myvar);
	console.log("After");
}


I expected to get an error with "myvar="changed!";" but it works. Did I misunderstand what is meant with primitives being immutable?

Thank you.

Reply With Quote
  #2  
Old January 2nd, 2013, 10:24 AM
AndrewSW's Avatar
AndrewSW AndrewSW is offline
JavaScript is not spelt java
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2011
Location: Landan, England
Posts: 743 AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 23 h 1 m 13 sec
Reputation Power: 164
Code:
var myvar = "hello, world!";
myvar = "changed!";


I know it seems that you are changing myvar but, technically, you are not. The 2nd line creates a new string that myvar now points to. The previous string "hello, world!" still exists (unaltered) in the computers' memory - but it's of no use as nothing points to it. (It will be removed from memory later by a process called garbage collection.)

Similarly,

Code:
var myvar = "Hello World!";
myvar = myvar + " Yo!";


does not modify myvar; the 2nd line (again) creates a new string. This is why string operations can be expensive.
Comments on this post
Kravvitz agrees!
__________________
"The mysql extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used." the docs

Reply With Quote
  #3  
Old January 2nd, 2013, 10:35 AM
littlebigman littlebigman is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2010
Posts: 28 littlebigman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 41 m 2 sec
Reputation Power: 0
Thanks for the info.

So primitive values cannot be changed in the sense that a new variable will be created on the fly, not in the sense that it would trigger an error as I expected.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Primitive values immutable or not?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap