Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Practice simple arrays by creating an array literal, using indices to access array members and use methods like push, join, pop and shift to manipulate the array.
Using let
and const
JavaScript used to have only one keyword for creating a variable: var
. For example, to create a variable named temperature with a value of 98.9 you could do this:
var temperature = 98.9
.
Two new keywords were introduced in ES2015 (a newer version of JavaScript): let
and const
. Most JavaScript programmers now use those two keywords. However, var
works and you'll still see it around a lot. To get familiar with these keywords follow these rules:
const
Use const
(which is short for constant) to store a value that won't change. For example, if you're setting the sales tax rate -- which won't change while your program is running you'd use const
. For example:
const taxRate = 7.5;
let
Use let
to store a value that changes while the program runs. For example, if you're keeping track of a changing score, or the number of times a person clicks on a web page, use let
:
let totalSales = 0;
As with var
you can store any type of data in a variable defined with let
or const
-- numbers, strings, arrays, objects, Boolean values, etc. If you are confused about whether to use let
or const
the rule of thumb is if you're in doubt, use const
.
To learn more, check out the workshop Defining Variables with let and const
Code for completed solution
// 1. Create a new array named languages that contains at least 7 programming languages.
let languages = ['JavaScript', 'Python', 'Java', 'C#', 'PHP', 'Swift', 'Ruby'];
// 2. Use console.log to display the number of elements in the array:
console.log(languages.length);
/* 3. Use console.log to list the first element from the array using an index value. */
console.log(languages[0]);
/* 4. Use console.log to list the last element from the array using an index value. */
console.log(languages[languages.length - 1]);
// 5. Use an array method to add an element to the END of the array.
languages.push('Go');
// 6. Use an array method to add an element to the BEGINNING of the array.
languages.unshift('Erlang');
/* 7. Log all of the elements in the array as a single string using the array .join() method */
console.log(languages.join(', '));
// 8. Use an array method to REMOVE the LAST element from the array.
console.log(languages.pop());
// 9. Use an array method to REMOVE the first element from the array.
console.log(languages.shift());
/* 10. Log all of the elements in the array as a single string using the array .join() method. Thisis the same as step 7 above */
console.log(languages.join(', '));
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up