Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #9720 > unrolled thread
| Started by | Archos <raul.san@sent.com> |
|---|---|
| First post | 2011-12-28 00:53 -0800 |
| Last post | 2011-12-28 15:44 +0200 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.javascript
Explicit zero-initialization of vars Archos <raul.san@sent.com> - 2011-12-28 00:53 -0800
Re: Explicit zero-initialization of vars "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2011-12-28 05:19 -0800
Re: Explicit zero-initialization of vars Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-12-28 16:48 +0100
Re: Explicit zero-initialization of vars "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2011-12-28 15:44 +0200
| From | Archos <raul.san@sent.com> |
|---|---|
| Date | 2011-12-28 00:53 -0800 |
| Subject | Explicit zero-initialization of vars |
| Message-ID | <7351867e-b6e5-457f-a080-bd03a2db9006@t30g2000vbx.googlegroups.com> |
Is it recommended make zero-initialization of vars explicit? By example: `var i = 0` instead of `var i` // for an integer `var s = ""` instead of `var s` // for a string
[toc] | [next] | [standalone]
| From | "Michael Haufe (TNO)" <tno@thenewobjective.com> |
|---|---|
| Date | 2011-12-28 05:19 -0800 |
| Message-ID | <f033690e-4c08-4c61-b872-94c2739fff74@l19g2000yqc.googlegroups.com> |
| In reply to | #9720 |
On Dec 28, 2:53 am, Archos <raul....@sent.com> wrote: > Is it recommended make zero-initialization of vars explicit? > > By example: > > `var i = 0` instead of `var i` // for an integer > `var s = ""` instead of `var s` // for a string No.
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2011-12-28 16:48 +0100 |
| Message-ID | <1948058.aqSPkdTlGX@PointedEars.de> |
| In reply to | #9722 |
Michael Haufe (TNO) wrote: > On Dec 28, 2:53 am, Archos <raul....@sent.com> wrote: >> Is it recommended make zero-initialization of vars explicit? >> >> By example: >> >> `var i = 0` instead of `var i` // for an integer >> `var s = ""` instead of `var s` // for a string > > No. It depends. PointedEars -- Danny Goodman's books are out of date and teach practices that are positively harmful for cross-browser scripting. -- Richard Cornford, cljs, <cife6q$253$1$8300dec7@news.demon.co.uk> (2004)
[toc] | [prev] | [next] | [standalone]
| From | "Jukka K. Korpela" <jkorpela@cs.tut.fi> |
|---|---|
| Date | 2011-12-28 15:44 +0200 |
| Message-ID | <jdf6f2$nbu$1@dont-email.me> |
| In reply to | #9720 |
2011-12-28 10:53, Archos wrote: > Is it recommended make zero-initialization of vars explicit? There is no implicit zero-initialization. If you declare a variable without initializing it, its initial value is the special value undefined, of the special type undefined. > By example: > > `var i = 0` instead of `var i` // for an integer > `var s = ""` instead of `var s` // for a string Without the initializers (= 0 and = ""), both i and s would come to life with the value undefined. It's not an integer value, it's no a string value, it's - of type undefined. You can see this if you check out this: var i; alert(i); alert(typeof i); It is generally a good idea to initialize a variable on declaration, if a natural and useful initial value can be assigned to it, such as 0 for a counter or "" for a variable into which some string will be composed by catenating strings. It is not a good idea to initialize a variable with a dummy value just to make it initialized. Every variable _is_ initialized by language rules in JavaScript, and the special value undefined is often more useful than a dummy neutral value like 0 or "". If your code runs wild and uses the value of a variable to which no proper value has been assigned, it's better to "see" the variable value as undefined, as it logically is. -- Yucca, http://www.cs.tut.fi/~jkorpela/
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web