...

View Full Version : New to Javascript



Democrazy
09-18-2011, 01:32 PM
I'm learning JavaScript. My suspicions for all these years would be I'd hate it, and it I was right. It seems I have to write half a page to perform something so simple.

Anyway, a question:

Do I have to end **** with a ; always?

{example;}

sampson1952
09-18-2011, 02:27 PM
I'm learning JavaScript. My suspicions for all these years would be I'd hate it, and it I was right. It seems I have to write half a page to perform something so simple.

Anyway, a question:

Do I have to end **** with a ; always?

{example;}
The semi-colon separates statements... they are not required at the end of all lines... only between statements.

Your example only had a single statement, therefore did not require the semi-colon (although it is permitted)... if you had something like {example1; example2}... then the semi-colon would be required.

Democrazy
09-18-2011, 02:36 PM
Thanks mate!

DaveyErwin
09-18-2011, 03:05 PM
I'm llearning JavaScript. My suspicions for all these years would be I'd hate it, and it I was right. It seems I have to write half a page to perform something so simple.

can you give an example of "to perform something so simple"



Anyway, a question:

Do I have to end **** with a ; always?

{example;}



There are places you must not
put semi-colons, here are two ...


<script>
var a = {hi:'hello',bye:'goodbye'};
for(var i in a)//<-- no semi-colon here!
{
alert(a[i]);
}
if(a)//<-- no semi-colon here!
alert(a.hi);
</script>

minkoko
09-18-2011, 04:18 PM
var a = {hi:'hello',bye:'goodbye'};

is it statement? end with semi-colon

can you explain me this string

{ hi: 'hello',bye;'goodbye'}

DaveyErwin
09-18-2011, 04:27 PM
can you explain me this string

{ hi: 'hello',bye;'goodbye'}

This is a javascript Object, it closely
resembles what is known as a "hash".


here is the same written
in a different way ....





<script>
var a = new Object();
a.hi = "Hello";
a.bye = "GoodBye";
for(var i in a)//<-- no semi-colon here!
{
alert(a[i]);
}
if(a)//<-- no semi-colon here!
alert(a.hi);

</script>




and here it is yet another way ...




<script>
var a = new Object();
a['hi'] = "Hello";
a['bye'] = "GoodBye";
for(var i in a)//<-- no semi-colon here!
{
alert(a[i]);
}
if(a)//<-- no semi-colon here!
alert(a.hi);

</script>

minkoko
09-18-2011, 05:22 PM
Thanks for pointing me about the array object

anyone explain me this loop

for(var i in a)

like this (var i=0;i<a.length;i++)

i don't understand ,need help

DaveyErwin
09-18-2011, 05:36 PM
Thanks for pointing me about the array object


The cose I posted does
not use javascript Array
it uses javascript Object





anyone explain me this loop

for(var i in a)

like this (var i=0;i<a.length;i++)

i don't understand ,need help



Array have a length as your
code demonstrates the
Object created in my code
has no length property so,
its properties are accessed
with the javascript version
of a "for each property in object"
loop



<script>
var a = {hi:'hello',bye:'goodbye'};
for(var i in a)//<-- no semi-colon here!
{
alert(a[i]);
}
if(a)//<-- no semi-colon here!
alert(a.hi);
alert(a.length);//<-- undefined
</script>

minkoko
09-18-2011, 06:20 PM
so,

i value is start with 0 and a value is 2 in your script loop ?

DaveyErwin
09-18-2011, 07:42 PM
so,

i value is start with 0 and a value is 2 in your script loop ?

No! Look at this...




<script>
var a = {hi:'hello',bye:'goodbye'};
for(var i in a)//<-- no semi-colon here!
{
alert(i);//<--first iteration alerts hi, second alerts bye
}

alert(a.length);//<-- alerts undefined
</script>

minkoko
09-19-2011, 01:46 AM
thanks DaveyErwin

i now understand about alert(a.length); don't had any value because it undefine
is it right?

DaveyErwin
09-19-2011, 02:12 AM
thanks DaveyErwin

i now understand about alert(a.length); don't had any value because it undefine
is it right?

Yes , if a variable is declared
but not set to anything,
it's default value is undefined.

var a;
alert(a);//<--alerts undefined

Also an object property that
has not been declared returns
undefined.

a={};
alert(a.g);//<--alerts undefined



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum