Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Banned
    Join Date
    Sep 2011
    Posts
    140
    Thanks
    17
    Thanked 0 Times in 0 Posts

    New to Javascript

    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;}

  • #2
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Democrazy View Post
    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.

  • Users who have thanked sampson1952 for this post:

    Democrazy (09-18-2011)

  • #3
    Banned
    Join Date
    Sep 2011
    Posts
    140
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Thanks mate!

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    Quote Originally Posted by Democrazy View Post

    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"
    Quote Originally Posted by Democrazy View Post

    Anyway, a question:

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

    {example;}
    There are places you must not
    put semi-colons, here are two ...

    Code:
    <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>

  • #5
    New Coder
    Join Date
    Aug 2010
    Location
    myeik
    Posts
    81
    Thanks
    6
    Thanked 5 Times in 5 Posts
    PHP Code:
    var = {hi:'hello',bye:'goodbye'}; 
    is it statement? end with semi-colon

    can you explain me this string

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

  • #6
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    Quote Originally Posted by minkoko View Post

    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 ....


    Code:
    <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 ...

    Code:
    <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>
    Last edited by DaveyErwin; 09-18-2011 at 03:38 PM.

  • #7
    New Coder
    Join Date
    Aug 2010
    Location
    myeik
    Posts
    81
    Thanks
    6
    Thanked 5 Times in 5 Posts
    Thanks for pointing me about the array object

    anyone explain me this loop
    PHP Code:
    for(var i in a
    like this (var i=0;i<a.length;i++)

    i don't understand ,need help
    Last edited by minkoko; 09-18-2011 at 04:27 PM.

  • #8
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    Quote Originally Posted by minkoko View Post

    Thanks for pointing me about the array object
    The cose I posted does
    not use javascript Array
    it uses javascript Object

    Quote Originally Posted by minkoko View Post


    anyone explain me this loop
    PHP Code:
    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


    Code:
    <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>

  • #9
    New Coder
    Join Date
    Aug 2010
    Location
    myeik
    Posts
    81
    Thanks
    6
    Thanked 5 Times in 5 Posts
    so,

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

  • #10
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    Quote Originally Posted by minkoko View Post
    so,

    i value is start with 0 and a value is 2 in your script loop ?
    No! Look at this...

    Code:
    <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>

  • #11
    New Coder
    Join Date
    Aug 2010
    Location
    myeik
    Posts
    81
    Thanks
    6
    Thanked 5 Times in 5 Posts
    thanks DaveyErwin

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

  • #12
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    Quote Originally Posted by minkoko View Post
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •