A variable is a place to store values
To declare (create) a variable, just type the word "var" and the variable name.
<script>
var numberOfKittens;
</script>
It is a good idea to give your variable a starting value. This is called initializing the variable.
<script>
var numberOfKittens = 5;
</script>
Once you have created a variable, you can use it in your code. Just type the name of the variable.
<script>
var numberOfKittens = 5;
console.log (numberOfKittens);
</script>
In your JS file, create a variable and give it a valid name and a value. Then, display the value.
Variables can be numbers, either integers or floats (decimals).
<script>
var numberOfKittens = 5;
var cutenessRating = 9.6;
</script>
The browser will automatically convert integers to floats if needed
Once you have numbers, you can do math with them!
<script>
var numberOfKittens = 5;
var numberOfPuppies = 4;
var numberOfAnimals = numberOfKittens + numberOfPuppies;
</script>
Example | Name | Result |
---|---|---|
-a | Negation | Opposite of a. |
a + b | Addition | Sum of a and b. |
a - b | Subtraction | Difference of a and b. |
a * b | Multiplication | Product of a and b. |
a / b | Division | Quotient of a and b. |
a % b | Modulus | Remainder of a divided by b. |
Create two variables and try some arithmetic operators. Don't forget to display your results!
Variables can be strings, groups of characters. You put your string in quotes.
<script>
var kittensName = 'Fluffy';
</script>
If you want to use a quote in your string, you'll need to "escape" it with a backslash.
<script>
console.log('I\'d like to use an apostrophe');
</script>
You can put strings together with a +, the concatenation operator.
<script>
var kittensName = 'Fluffy ';
var fullName = kittensName + ' McDougle';
console.log(fullName); //Outputs 'Fluffy McDougle'
</script>
You can also use += to add things to the end of a string.
<script>
var kittensName = 'Admiral ';
kittensName += ' Snuggles';
console.log(kittensName); //Outputs 'Admiral Snuggles'
</script>
Create two variables, a first name and a last name, and then put them together to make a full name. Don't forget to display your results!
Functions are separable, reusable pieces of code.
First, declare the function.
<script>
function turtleFact() {
console.log('A turtle\'s lower shell is called a plastron.');
}
</script>
Then, use it as many times as you want!
<script>
turtleFact();
</script>
Functions can accept input values, called arguments.
<script>
function callKitten (kittenName){
console.log('Come here, ' + kittenName + '!');
}
callKitten ('Fluffy'); //outputs 'Come here, Fluffy!'
function addNumbers(a, b) {
console.log(a + b);
}
addNumbers(5,7); //outputs 12
addNumbers(9,12); //outputs 21
</script>
You can also pass variables into functions. These variables do not need to have the same name as the function arguments.
<script>
function addOne(inputNumber){
var newNumber = inputNumber + 1;
console.log('<p>You now have ' + newNumber);
}
//Declare variables
var numberOfKittens = 5;
var numberOfPuppies = 4;
//Use them in functions
addOne(numberOfKittens);
addOne(numberOfPuppies);
</script>
Turn the code you wrote to output someone's full name into a function, then use it.
You can have a function give you back a value.
<script>
function addNumbers(num1, num2){
return num1 + num2;
}
var resultOfAddingNumbers = addNumbers(2,2);
console.log(resultOfAddingNumbers);
function subtractNumbers(num1, num2){
num1 - num2;
}
var resultOfSubtractingNumbers = subtractNumbers(2,2);
console.log(resultOfSubtractingNumbers);
</script>
Return will immediately end a function.
Add a return statement to your name function. Use that function to set the value of a variable.