Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-31-2012, 04:11 AM   PM User | #1
jhispro97
New to the CF scene

 
Join Date: Dec 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
jhispro97 is an unknown quantity at this point
Unhappy Problems with functions and variables

Hey, I've just started coding and i need help. Can anyone tell me whats wrong with these lines of codes please?

[CODE]
var multiplied;

var timesTwo = function(number) {

var multiplied = number * 2;
};

timesTwo(4);

console.log (multiplied);
[CODE]

It doesnt print out the value of multiplied, instead it says that it is undefined. What have i done wrong?
jhispro97 is offline   Reply With Quote
Old 12-31-2012, 04:28 AM   PM User | #2
tempz
Regular Coder

 
Join Date: Jul 2012
Location: London
Posts: 436
Thanks: 4
Thanked 80 Times in 80 Posts
tempz is an unknown quantity at this point
What do you want to do?

Multiple two numbers?
tempz is offline   Reply With Quote
Old 12-31-2012, 04:30 AM   PM User | #3
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,765
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Lightbulb

Quote:
Originally Posted by jhispro97 View Post
Hey, I've just started coding and i need help. Can anyone tell me whats wrong with these lines of codes please?

Code:
var multiplied;

var timesTwo = function(number) {
    
    var multiplied = number * 2;
    };

timesTwo(4);

console.log (multiplied);
It doesnt print out the value of multiplied, instead it says that it is undefined. What have i done wrong?
You have two versions of the variable 'multiplied'.
One is local and will work fine if displayed within the function.
However, you are not doing that.

The second version of 'multiplied' is a global varible and is unassigned.
This is the one you are trying to display in the console log.

BTW, you second [ code] tag should have a /code to display properly.
jmrker is offline   Reply With Quote
Users who have thanked jmrker for this post:
jhispro97 (12-31-2012)
Old 12-31-2012, 08:57 AM   PM User | #4
jhispro97
New to the CF scene

 
Join Date: Dec 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
jhispro97 is an unknown quantity at this point
Smile

Oh. I realized it now. Thanks for helping out btw. And sorry for the misusing of the [ code] tag.
jhispro97 is offline   Reply With Quote
Old 12-31-2012, 02:03 PM   PM User | #5
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,765
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
You're most welcome.
Happy to help.
Good luck!
jmrker is offline   Reply With Quote
Reply

Bookmarks

Tags
coding, function, functions, java, variables

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:29 AM.


Advertisement
Log in to turn off these ads.