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 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

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
INTERSECT and EXCEPT in PostgresPostgresWhat are Modules and Components in Angular?AngularHow to add before after image effect using pure CSS?CSSHow to create a Progress Bar using JavaScript?JavaScriptIntroduction to components and templates Part 1: Component MetadataAngularHow to add Local State to a Class in React?ReactHow to use data-* Attributes in HTML?HTMLHow to Use password_hash and password_verify to Secure Your User’s Data (Especially Passwords)?PHPHow to add Date and Time picker in HTML and get the values using JavaScript?HTMLHow to disable the Recovery Mode in WordPress?WordPressHow to hide a DIV on clicks outside of it using jQuery?jQueryHow to create a function in SCSS (Sass)?SCSS