Javascript 4: Variable
Variable atau pemboleh ubah adalah satu value yang kita declare value tersebut untuk memberi nilai pada pemboleh ubah tersebut.var myAge=27; console.log(myAge);
// PAPARAN: 27
Kita declare myAge dengan memberi arahan var myAge, kemudian kita masukkan nilai dengan arahan =27; Untuk coding yang complex, kita perlu menyimpan value siap-siap kedalam variable dengan specific dan case sensitive nama yang senang diingati, dan kita akan panggil variable ini di masa yang mana perlu padanya.
Kita telah melihat bagaimana mencipta variable. Sekarnag bagaimana untuk menggunakannya? Ianya berguna ketika anda memasukkan sesuatu nilai kedalam variable dan variable tersebut digunakan untuk formula atau pengiraan berasaskan variable yang telah dimasukkan. For example:
var myName = "Encraption Team"; myName.substring(0,5)
Jika dilihat disini, myName sekarang adalah diwakili oleh value 'Encraption Team'.
myName.substring(0,5)
pada zahirnya kelihatan ianya myName, tetapi ianya sebenarnya memegang value seperti dibawah
"Encraption Team".substring(0,5)
iaitu nilainya adalah Encraption Team. Contoh penggunaan Variable adalah memberi value pada variable myCountry sebagai value Malaysia dan melakukan operasi keatas variable tersebut
// Declare a variable on line 3 called // myCountry and give it a string value. var myCountry = "Malaysia"; // Use console.log to print out the length of the variable myCountry. console.log( myCountry.length); // Use console.log to print out the first three letters of myCountry. console.log(myCountry.substring(0,3) );
Paparan: 8
Mal
// On line 2, declare a variable myName and give it your name. var myName = "Mr Encraptor"; // On line 4, use console.log to print out the myName variable. console.log (myName); // On line 7, change the value of myName to be just the first 2 // letters of your name. var myName = "Mr"; // On line 9, use console.log to print out the myName variable. console.log (myName);
Paparan:
Mr Encraptor
Mr
On line 1, create a variable myColor and give it a string value.
On line 2, print the length of myColor to the console.
var myColor = "Green"; console.log(myColor.length);
Paparan:
5
// Not sure where to begin? Check the Hint!
var myNumber=27;
if
(myNumber === 27)
{
console.log("I finished my first course!");
}
else
{
console.log("I cant finished my first course!");
}
Paparan:
I finished my first course!
// Check if the user is ready to play!
confirm("I am ready to play!");
var age = prompt("What's your age");
if (age<=13)
{
console.log ( "You cant play this game");
}
else
{
console.log ("please play it safely");
}
Jika user masukkan melebihi 13, mereka boleh bermain game ini, sebaliknya mereka tidak dibenarkan bermain game ini.
// Check if the user is ready to play!
confirm("I am ready to play!");
var age = prompt("What's your age");
if (age<=13)
{
console.log ( "You cant play this game");
}
else
{
console.log ("please play it safely");
}
console.log ("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'");
console.log ("Suddenly, Bieber stops and says, 'Who wants to race me?'");
var userAnswer=prompt("Do you want to race Bieber on stage?");
if (userAnswer==="yes")
{
console.log ( "You and Bieber start racing. It's neck and neck! You win by a shoelace!");
}
else
{
console.log ("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'");
}
var feedback=prompt("Please rate our game");
if (feedback>=8)
{
console.log("Thank you! We should race at the next concert!");
}
else
{
console.log("I'll keep practicing coding and racing.");
}
Paparan:

