Learn
Sometimes when we work with data, we find that information is missing. Let’s imagine that you are writing a program that displays the first letter of a user’s middle name.
let firstName = "Franklin" let middleName = "Delano" let lastName = "Roosevelt"
For this user, his middle name is “Delano”, so our program should print “D”. But what about this user?
let firstName = "George" let middleName = "" let lastName = "Washington"
His middle name is an empty string that doesn’t contain any characters! How can we find the first character if there are no characters in his middle name at all?
Enter optionals. An optional represents a variable that might be absent. We can say that the first letter in a user’s middle name is an optional character. Either it is a real character or nil
which represents the absence of a value. In this lesson, you’ll learn how to define and handle optional values.
Take this course for free
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.