← Back to Lua Course | Chapter 2: Variables & Types | Lesson 1 of 7

Variables और Assignment

एक variable एक नाम है जो एक value holds करता है, और आप किसी भी समय यह बदल सकते हैं कि यह क्या holds करता है।

Variables और Assignment

एक single equals sign से assign करें। Variables का कोई declared type नहीं होता, सिर्फ values का होता है, इसलिए same variable एक number hold कर सकता है और बाद में एक string। Lua multiple assignment support करता है, इसलिए a, b = b, a बिना किसी temporary के दो values swap करता है।

Note: Names case sensitive हैं और इनमें letters, digits, और underscores हो सकते हैं लेकिन एक digit से शुरू नहीं हो सकते।

उदाहरण: Variables and assignment

lua
local x = 10
x = "now a string"
print(x)

local a, b = 1, 2
a, b = b, a
print(a, b)

local p, q, r = 1, 2
print(p, q, r)

-- Output:
-- now a string
-- 2	1
-- 1	2	nil
{# 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. end या function जैसा एक reserved word नाम की तरह इस्तेमाल करना
  2. यह उम्मीद करना कि एक declared type enforce होगा
  3. यह मान लेना कि a, b = 1 b को value 1 देता है
चैप्टर सारांश
  • = से assign करें
  • Variables किसी भी type की value hold कर सकते हैं
  • Multiple assignment values swap करता है
  • Missing values nil बन जाती हैं
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 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.