Yogesh Chauhan's Blog

What is the difference between let and var in Swift?

in Swift on February 28, 2020

let = Constant and var = variables. Period. As simple as that. Values of constants can not be changed but values of variables can be changed.

Constants and Variables

Constants and variables associate a name with a value of a particular type (such as the number 10 or the string "Hello"). The value of a constant can’t be changed once it’s set, whereas a variable can be set to a different value in the future.

Declaring Constants and Variables

Constants and variables must be declared before they’re used. You declare constants with the let keyword and variables with the var keyword. Here’s an example of how constants and variables can be used to track the number of login attempts a user has made:

let maximumNumberOfLoginAttempts = 10
var currentLoginAttempt = 0

In the example above, value of maximumNumberOfLoginAttempts can not be changed but value of currentLoginAttempt can and will be changed as user tries to log in each time.

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
CSS Overflow Property with ExamplesCSSHow to compress images with gulp in WordPress?WordPressWordPress: How to find all posts from a custom post type with multiple custom fields values?WordPressHow to add today’s date in HTML input date value using JavaScript?JavaScriptHow to get recent posts in WordPress?WordPressPostgreSQL BETWEENPostgresWhat is a Strict Requirement in PHP 7 Function Declarations?PHPHow to remove border from :visited image, link or button?CSSMIN, MAX, COUNT, AVG and SUM in SQLSQL/MySQLExplanation of PostgreSQL PgAdmin interfacePostgresHow to add transparent text on top of an image using CSS?CSSNew to  Drupal? Here’s What You Need to KnowDrupal