We learned one way to create an array, but there’s another.

We can also declare and initialize an array with a list of values, separated by commas, and surrounded by a pair of square brackets.

The syntax to create an array this way looks like:

var name = [list of values]

Suppose we want to create an array called location with Codecademy HQ’s latitude and longitude:

var location = [40.7245, -73.9979]

Due to Swift’s built-in type inference, the type of the array is inferred. To be explicit:

var location: [Double] = [40.7245, -73.9979]

Let’s try creating arrays using this new approach.



Suppose the Tokyo Subway costs are as follows:

Ticket Adult Child
24-hour ¥800 ¥400
48-hour ¥1200 ¥600
72-hour ¥1500 ¥750

Create an array named subwayAdult and initialize it with 800, 1200, and 1500.

Print it out using print().


Initialize subwayChild with 400, 600, and 750. This time, be explicit with the type.

Then print it out.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?