Talk:JavaScript syntax: Difference between revisions

Content deleted Content added
Removed maintenance template—links fixed
m Task 70: Update syntaxhighlight tags - remove use of deprecated <source> tags
Line 108:
Should it not be explained that the following:
 
<sourcesyntaxhighlight lang="JavaScript">
if(x==1){
something()
Line 115:
somethingelse();
}
</syntaxhighlight>
</source>
the else if works like a shorthand if (without {})
 
<sourcesyntaxhighlight lang="JavaScript">
if(x==1)
do something here
Line 131:
}
}
</syntaxhighlight>
</source>
Sorry if that didn't make sense, I find it hard to explain.
 
Line 145:
== For loop: end-condition? ==
 
<sourcesyntaxhighlight lang="JavaScript">
for (initial;end-condition;loop statement) {
/*
Line 153:
*/
}
</syntaxhighlight>
</source>
 
The condition should be called ''while-condition'', or just ''condition'', but never ''end-condition''. As in every C-like language, the for{} loop cycles '''while''' the condition is satisfied. --[[User:Comocomocomocomo|Como]] ([[User talk:Comocomocomocomo|talk]]) 14:28, 23 October 2008 (UTC)
Line 208:
The code could be changed to also illustrate the ternary operator <tt>? :</tt> :
 
<sourcesyntaxhighlight lang="JavaScript">
return diff > 0 ? gcd(segmentB, diff) : gcd(segmentA, -diff)
</syntaxhighlight>
</source>
 
Since the code might be copied, it would be well to change it, after testing, to the more efficient method, in which the second argument to the internal <tt>gcd</tt> is either <tt>A mod B</tt> or <tt>B mod A</tt>, which is approximately equally easy to read; or to the wondrous non-recursive form :
 
<sourcesyntaxhighlight lang="JavaScript">
function GCD(U, V) { // or HCF
while (true) {
if (!(U %= V)) return V
if (!(V %= U)) return U } }
</syntaxhighlight>
</source>
 
And, for the same reason, the alternative acronym HCF should be included. [[Special:Contributions/82.163.24.100|82.163.24.100]] ([[User talk:82.163.24.100|talk]]) 20:01, 24 July 2009 (UTC)
Line 268:
 
<div style="font-size:75%;line-height:150%;margin-left:2.5em;padding-bottom:1.5em;">
<sourcesyntaxhighlight lang="JavaScript">
// Automatic type coercion
//Boolean operands will be converted to a number, if possible, or to a string (if the other is a string)
Line 286:
alert(true === 1); // false
alert(true === "1"); // false
</syntaxhighlight>
</source>
 
Examples with "falsy" and "truthy" values.
 
<sourcesyntaxhighlight lang="JavaScript">alert(3?true:false); // true
alert(true == 3); // false
// 3 is logically true(known as "truthy"), as are all non-0 non-nan numbers. However, it is not "true", even with type coercion.
Line 298:
 
alert(false == +'NaN'); // false (shorthand for getting a NaN value)
</syntaxhighlight>
</source>
</div>
:I think some of the permutations could potentially be eliminated but it's necessary to show, for example, that neither <code>true==2</code> nor <code>false==2</code> behave the way one might expect.
Line 333:
== Function example code misleading ==
 
<sourcesyntaxhighlight lang="JavaScript">
var obj1 = {a : 1};
var obj2 = {b : 2};
Line 342:
foo(obj1, 3); // Does not affect obj1 at all. 3 is additional parameter
alert(obj1.a + " " + obj2.b); // writes 1 3
</syntaxhighlight>
</source>
 
This is misleading because obj1 can be modified by the function. It is quite odd (in an explanation) to pass a parameter to a function and then ignore it.
Consider:
<sourcesyntaxhighlight lang="JavaScript">
var obj1 = {a : 1};
var obj2 = {b : 2};
Line 356:
foo(obj1, 3); // Does affect obj1. 3 is additional parameter
alert(obj1.a + " " + obj2.b); // writes 7 3
</syntaxhighlight>
</source>
Because the object parameter is passed by reference it can be modified.
This is OR for me. I am just learning. But I was surprised that obj1 can be modified in the function, by p.a=7, yet p=obj2 does not affect obj1. This should be explained. [[User:QuentinUK|QuentinUK]] ([[User talk:QuentinUK|talk]]) 02:27, 28 June 2011 (UTC)
Line 406:
 
The following
<sourcesyntaxhighlight lang="JavaScript">
alert( !0 === Boolean( !0 ) === !!1 === Boolean( 1 ) === true );
alert( !!0 === Boolean( 0 ) === !1 === Boolean( !1 ) === false );
alert( !"" === Boolean( !"" ) === !!"s" === Boolean( "s" ) === true );
alert( !!"" === Boolean( "" ) === !"s" === Boolean( !"s" ) === false );
</syntaxhighlight>
</source>
Gives the impression that equality can be chained together. A===B===C being true when they are all equal, this is not the case. [[User:QuentinUK|QuentinUK]] ([[User talk:QuentinUK|talk]]) 10:57, 11 August 2011 (UTC)