MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Assignment & Ternary Operator0%
Study Materials
JavaScript for BeginnersProgress 0%
1.1What is JavaScript?1.2How JavaScript Works in Browser1.3Setting up Environment: VSCode, Live Server
3.1Arithmetic Operators3.2Comparison Operators3.3Logical Operators3.4Assignment & Ternary Operator
HomeTutorialsJavaScript for BeginnersAssignment & Ternary Operator
Conditional Statements: if, else if, else

Assignment & Ternary Operator

Beginner12 min read•Updated: 2026-09-15•By Er. Sumit Kumar (MSK Institute)
Study Materials

Assignment & Ternary Conditional Operator

Assignment operators update the value stored inside variables, while the ternary operator provides a concise, elegant way to write inline conditional expressions.


1. Assignment Operators

The basic assignment operator is the equals sign (=). JavaScript also provides compound assignment operators that combine an arithmetic operation with assignment:

OperatorNameEquivalent ToExample
=Simple Assignmentx = ylet score = 100;
+=Addition Assignmentx = x + yscore += 10; (Now 110)
-=Subtraction Assignmentx = x - yscore -= 20; (Now 90)
*`=`**Multiplication Assignmentx = x * yscore *= 2; (Now 180)
/=Division Assignmentx = x / yscore /= 3; (Now 60)
%=Modulus Assignmentx = x % yscore %= 7; (Now 4)
`=`**Exponentiation Assignmentx = x ** yscore **= 2; (Now 16)

String Concatenation Assignment (+=):

1let message = "Hello";
2message += " World";
3message += "!";
4console.log(message); // "Hello World!"

2. The Ternary Operator (condition ? expr1 : expr2)

The ternary operator is the only operator in JavaScript that takes three operands. It serves as a compact, inline shortcut for an if...else statement:

1condition ? value_if_true : value_if_false

Comparison: if...else vs. Ternary

1const age = 20;
2let statusMessage;
3 
4// Using traditional if...else:
5if (age >= 18) {
6 statusMessage = "Adult";
7} else {
8 statusMessage = "Minor";
9}
10 
11// Using concise ternary operator:
12const statusMessage = age >= 18 ? "Adult" : "Minor";

Why Developers Love the Ternary Operator:

Because the ternary operator is an expression (it returns a value), you can assign it directly to a const variable, pass it as a function argument, or embed it directly inside template literals:

1const userRole = "admin";
2console.log(`Welcome, ${userRole === "admin" ? "Administrator" : "Guest"}!`);

3. Nested Ternary Operators (Use With Caution!)

You can chain ternary operators for multiple conditions, but be careful not to hurt code readability:

1const score = 85;
2 
3const grade = score >= 90 ? "A"
4 : score >= 80 ? "B"
5 : score >= 70 ? "C"
6 : "F";
7 
8console.log(grade); // "B"
Pro Tip
[!TIP] If a nested ternary exceeds 2 levels of depth, switch to a standard if...else or switch statement for better team readability.

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What is the equivalent of x += 5?
Question 2
How many operands does the ternary operator take?
Question 3
What is the result of const result = (10 > 5) ? "Yes" : "No";?
Question 4
Why is the ternary operator preferred over if...else when initializing const variables?
Question 5
What is the value of x after running let x = 12; x %= 5;?
Next Lesson

Conditional Statements: if, else if, else

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Logical OperatorsConditional Statements: if, else if, else

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Beginners with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext