← Back to Swift Course | Chapter 2: Variables & Types | Lesson 1 of 8

Variables और Constants

एक variable एक labeled box है जिसमें आप चीज़ें डाल सकते हैं और बाद में बदल सकते हैं, जबकि एक constant एक labeled box है जिसे आप एक बार भरते हैं और कभी बदल नहीं सकते।
Syntax
swift
var variableName = initialValue    // can change
variableName = newValue

let constantName = value           // cannot change

Declaring Variables with var

एक variable var के बाद एक नाम और एक value से declare होता है; इसकी value को बाद में फिर = उपयोग करके बदला जा सकता है।

उदाहरण: Declaring Variables with var

markup
// Declare a mutable variable `score`, initialized to `10`
var score = 10
// Print "Initial score: \(score)" to the console
print("Initial score: \(score)")
score = 25
// Print "Updated score: \(score)" to the console
print("Updated score: \(score)")

Declaring Constants with let

एक constant let से declare होता है। एक बार इसकी एक value हो जाए, उस value को program की जीवनभर के लिए कभी नहीं बदला जा सकता।

Note: अगर आप एक let को reassign करने की कोशिश करें, Swift एक clear error message के साथ compile करने से मना कर देगा।

उदाहरण: Declaring Constants with let

markup
let pi = 3.14159
print("Pi is approximately \(pi)")

Why Prefer let

Swift जहां भी संभव हो let उपयोग करने को प्रोत्साहित करती है क्योंकि immutable values के बारे में reason करना आसान है और code में कहीं और accidental changes रोकते हैं।

Note: Swift compiler var को let में बदलने का सुझाव देगा अगर यह notice करे कि एक variable कभी reassign नहीं हुआ।

उदाहरण: Why Prefer let

markup
// Declare a constant `maxPlayers`, set to `4`
let maxPlayers = 4
// Declare a mutable variable `currentPlayers`, initialized to `1`
var currentPlayers = 1
currentPlayers += 1
// Print "\(currentPlayers) of \(maxPlayers) players joined" to the console
print("\(currentPlayers) of \(maxPlayers) players joined")
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक ऐसी value के लिए var उपयोग करना जो कभी नहीं बदलती; Swift का compiler आपको इसे let में बदलने की warn भी करेगा।
  2. एक let constant को इसकी value दिए जाने के बाद reassign करने की कोशिश करना, जो एक compile error है।
  3. यह भूल जाना कि एक constant को उपयोग होने से पहले एक value दी जानी चाहिए, भले ही ज़रूरी नहीं कि बिल्कुल declaration line पर हो।
चैप्टर सारांश
  • var एक variable declare करता है जिसकी value सेट होने के बाद बदल सकती है।
  • let एक constant declare करता है जिसकी value एक बार assign होने के बाद बदल नहीं सकती।
  • default रूप से let पसंद करें, और सिर्फ तब var उपयोग करें जब value को genuinely बदलने की ज़रूरत हो।
  • Variables और constants दोनों को उपयोग से पहले एक value से initialize किया जाना चाहिए।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.