Swift: Declare a Variable

Declare a Variable in Swift:

Assume we’re taking a test and want to keep track of the student’s name, their marks (which range from 0 to 10), grade, and so on. For this purpose declare a variable in Swift.

We must declare or create a variable before we can utilise it.

So, to declare a marks variable and set it to 0, we must write:

var marks = 0;
  • var is a variable declaration keyword.
  • marks is the name of the variable.
  • = is the assignment operator.
  • 0 is the value of the variable.

A single equal symbol = in Swift indicates to “assign” values (not compare). We’ve given the marks variable a value of 0 in the code above.

As a result, we can now print the variable marks value.

print(marks);

//Output : 0

Note: Variable names should be written in camelCase format.

Helpful Tools:

Here are some useful tools to help you along your journey!

Setting up an IDE (Integrated Development Environment) can be difficult for beginners. The Online Compiler will enable you to run your code inside your browser without the need to install an IDE. If you need a more detailed explanation of a specific topic, the best place to find answers is in the Official Documentation.

Scroll to Top
%d bloggers like this: