For this section, let’s work in the console, so have that open(view → developer → javascript console OR option command J). Let’s also something other than this document to preview, I’ll use our class site.
Variables
Variables in javascript are defined using the keyword var or let. Variables are custom names for a particular action so that you have an easier time remembering how to use it.
Try writing var myDate = new Date() and then type myDate to reference your variable.
We’re defining a variable to the date object that exists in JavaScript.
When working with JavaScript code, you’ll work with a combination of variables(custom) and default JavaScript functionality(like Date() which is a function)
// Create and set a variable to any value you want
var className ='Art of the Web';
var studioTeacher ='Nika';
var currentWeekNumber =12;
var stayInSchool =true;
var myArray = [1, 2, 3, 4];
Arrays
Arrays are collections of values. Think of an array as a list. You can add and remove items from the list, in addition to a number of other functions that make working with arrays easier.
An array is defined as a series of comma-separated values inside brackets
Current Events
JavaScript Concepts
Variables
// Create and set a variable to any value you want
var className = 'Art of the Web';
var studioTeacher = 'Nika';
var currentWeekNumber = 12;
var stayInSchool = true;
var myArray = [1, 2, 3, 4];
Arrays
// An empty array
[]
// An array with one item in it
['hello world']
// An array with many items in it.
// The type of data in an array can vary.
['hello', 'world', 1, 2, true, false, 'lorem ipsum']
var myArray = ['apple', 'orange', 'banana', 'peach'];
var firstItem = myArray[0]; // 'apple'
var lastItem = myArray[3]; // 'peach'
// A better way to get the last item in an array is to
// use the length of the array itself
lastItem = myArray[myArray.length - 1]; // 'peach'
Math Random