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 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
JavaScript: how to detect a browser using the user agent?JavaScriptMiddleware in NextJSNextJSLearn how to add Scroll Indicator using CSS and JavaScript?CSSJavaScript Data Types and Data Structures Things to RememberJavaScriptThe actual difference between indexOf() and search() in JavaScriptJavaScriptHow to Commit and Rollback Changes in SQL?SQL/MySQLHow to implement NSNumberFormatter in Swift for currency?SwiftList of social media icon logo color codes in HEXMiscellaneousFETCH clause in PostgreSQLPostgresHow to vertically and horizontally align text and image block (without flex or grid) in CSS?CSSStyling Lists with CSSCSSHow Do You Make a Private VPN?Miscellaneous