Post: JavaScript Question from a Nub Here
05-11-2017, 01:32 PM #1
Hydrogen
Super Mod
(adsbygoogle = window.adsbygoogle || []).push({}); Hey guys, so I'm finally learning code for the first time ever myself since my AP Computer Science Teacher in 11th is pretty bad at teaching us. So, I'm taking advantage of myself to do better. Now, I done HTML completely and a bit of CSS, but It's time to step up a notch.

So here on my JS Code, I have a IF/Else Statement.

    var name = "Jon";
if (name.length > 9)
{
console.log("You're bigger Jon!");
}
else
{
console.log("The condition is false");
}


On Line 8 for the else statement, do I always have to add a semi-colon after the condition? I mean, I'm ending the function there since I'm calling it. Since the statement will have a probability of being false, we call it if the first condition is wrong.... correct?
05-11-2017, 04:31 PM #2
Algebra
[move]mov eax, 69[/move]
Originally posted by Hydrogen View Post
Hey guys, so I'm finally learning code for the first time ever myself since my AP Computer Science Teacher in 11th is pretty bad at teaching us. So, I'm taking advantage of myself to do better. Now, I done HTML completely and a bit of CSS, but It's time to step up a notch.

So here on my JS Code, I have a IF/Else Statement.

    var name = "Jon";
if (name.length > 9)
{
console.log("You're bigger Jon!");
}
else
{
console.log("The condition is false");
}


On Line 8 for the else statement, do I always have to add a semi-colon after the condition? I mean, I'm ending the function there since I'm calling it. Since the statement will have a probability of being false, we call it if the first condition is wrong.... correct?


I was told you do. Another thing is you don't need to include the var attribute when declaring a value. You only have to do this when using strict mode "use strict"; or if you feel it helps you better. Also I didn't spend to long on Javascript only like a month or so and still need to teach myself more advanced shit lol.
Last edited by Algebra ; 05-11-2017 at 04:35 PM.
05-11-2017, 05:07 PM #3
Hydrogen
Super Mod
Originally posted by Algebra View Post
I was told you do. Another thing is you don't need to include the var attribute when declaring a value. You only have to do this when using strict mode "use strict"; or if you feel it helps you better. Also I didn't spend to long on Javascript only like a month or so and still need to teach myself more advanced shit lol.


What do you mean not including the variable atr? Can show me it in BB Code?

The following user thanked Hydrogen for this useful post:

Algebra
05-11-2017, 05:17 PM #4
Tustin
Balls of Steel
Originally posted by Hydrogen View Post
What do you mean not including the variable atr? Can show me it in BB Code?

He's saying if you use the directive "use strict", it enforces more strict rules when declaring stuff. Personally I wouldn't bother using it and just stick with using var/let when declaring a variable because it's a lot easier to read

    
"use strict"
myVar = "hi"; //will fail
var newVar = "hi"; //wont fail


without "use strict"
    
myVar = "hi"; //wont fail
console.log(myVar); //will output hi

The following user thanked Tustin for this useful post:

Hydrogen
05-11-2017, 05:20 PM #5
Algebra
[move]mov eax, 69[/move]

Originally posted by Hydrogen View Post
Hey guys, so I'm finally learning code for the first time ever myself since my AP Computer Science Teacher in 11th is pretty bad at teaching us. So, I'm taking advantage of myself to do better. Now, I done HTML completely and a bit of CSS, but It's time to step up a notch.

So here on my JS Code, I have a IF/Else Statement.

    var name = "Jon";
if (name.length > 9)
{
console.log("You're bigger Jon!");
}
else
{
console.log("The condition is false");
}


On Line 8 for the else statement, do I always have to add a semi-colon after the condition? I mean, I'm ending the function there since I'm calling it. Since the statement will have a probability of being false, we call it if the first condition is wrong.... correct?



    
var name = "Jon";
if (name.length > 9)
{
console.log("You're bigger Jon!");
}
else
{
console.log("The condition is false");
}

What I would do is this

<script type="text/javascript">

"use strict";

var name = "Jon";

if (name.length > 1)
{
console.log("You're bigger Jon!")
}
else
{
console.log("The condition is false")
}

</script>

or

<script type="text/javascript">

name = "Jon";

if (name.length > 1)
console.log("You're bigger Jon!")
else
console.log("The condition is false")

</script>
05-11-2017, 05:28 PM #6
Hydrogen
Super Mod
Originally posted by Algebra View Post




    
var name = "Jon";
if (name.length > 9)
{
console.log("You're bigger Jon!");
}
else
{
console.log("The condition is false");
}

What I would do is this

<script type="text/javascript">

"use strict";

var name = "Jon";

if (name.length > 1)
{
console.log("You're bigger Jon!")
}
else
{
console.log("The condition is false")
}

</script>

or

<script type="text/javascript">

name = "Jon";

if (name.length > 1)
console.log("You're bigger Jon!")
else
console.log("The condition is false")

</script>


Originally posted by Tustin View Post
He's saying if you use the directive "use strict", it enforces more strict rules when declaring stuff. Personally I wouldn't bother using it and just stick with using var/let when declaring a variable because it's a lot easier to read

    
"use strict"
myVar = "hi"; //will fail
var newVar = "hi"; //wont fail


without "use strict"
    
myVar = "hi"; //wont fail
console.log(myVar); //will output hi


so something like this?

    nameType = "Jon";
if (nameType.length > 9)
{
console.log("You're bigger Jon!");
}
else
{
console.log("The condition is false");
}

The following user thanked Hydrogen for this useful post:

Algebra
05-11-2017, 05:30 PM #7
Algebra
[move]mov eax, 69[/move]

Originally posted by Hydrogen View Post
so something like this?

    nameType = "Jon";
if (nameType.length > 9)
{
console.log("You're bigger Jon!");
}
else
{
console.log("The condition is false");
}



Yeah bro btw them semi colons are optional more or less Smile

The following user groaned Algebra for this awful post:

05-11-2017, 05:33 PM #8
Hydrogen
Super Mod
Originally posted by Algebra View Post




Yeah bro btw them semi colons are optional more or less Smile


I thought I needed the semi's to call the functions of the conditions of IF/Else?
05-11-2017, 05:34 PM #9
Algebra
[move]mov eax, 69[/move]
Originally posted by Hydrogen View Post
I thought I needed the semi's to call the functions of the conditions of IF/Else?


You do but not with with the instructions you gave and most 1 liners lol

The following user thanked Algebra for this useful post:

Hydrogen
05-11-2017, 05:35 PM #10
Algebra
[move]mov eax, 69[/move]
I suggest adding them all the time don't mind me I only add them when it's necessary

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo