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 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #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

#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 #Jan 14 How to display a student’s individual transcript in Colleague? #Jan 11 How to install PuTTY on a MacOS?
You might also like these
How to make a curtain slider using jQuery and CSS?CSSHow to merge arrays in JavaScript?JavaScriptCan we execute conditions in SQL?SQL/MySQLHow to set the permissions on all files and folders in Mac?MiscellaneousHow Routing works in Angular?Angularaddcslashes() and addslashes() String Functions in PHPPHPWhat is Etrieve Flow?MiscellaneousCreate a simple bar chart and column chart using pure CSS gridCSSDROP DATABASE (remove a database) from PostgresPostgresperspective property in CSSCSSWhat is XP (Extreme Programming) and When should it be used?MiscellaneousHow to show and hide an element on click in React?React