Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
720 views
in Technique[技术] by (71.8m points)

discord - How to change a global variable within an if else statement?

I have declared a variable beforehand called:

var mathsLesson = 'https://www.link1.com';

The value of this variable will change

if (message.content === '!change') {           //this checks if the user messages !change
   mathsLesson = 'https://www.link2.com'
   message.channel.send(mathsLesson);          //This outputs www.link2.com
}

However when I output mathsLesson outside the if statement, it shows 'www.link1.com' instead of 'www.link2.com' which I wanted. How do I make it output 'www.link2.com'? Thanks in advance!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You shouldnt be using a VAR or CONST if the value of the variable tends the change. Dont use VAR because its pretty old and has many shortcomings change the var to let if you wanat the variable globally you can always export a function to return its value andmany other options

let mathsLesson = 'https://www.link1.com';
if (message.content === '!change') {           //this checks if the user messages !change
   let mathsLesson = 'https://www.link2.com'
   message.channel.send(mathsLesson);          //This outputs www.link2.com
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...