JavaScript Syntax & Basic Operators
Welcome to today’s kahani! Imagine JavaScript as a magic language you use to tell the computer what to do. But here’s the catch – you’ve got to say things just right, or the computer won’t understand! Today, we're talking about JavaScript syntax and basic operators, the words and symbols that help you communicate clearly in code.
Scene 1: The Syntax Club
Picture this: You walk into “The Syntax Club,” a place where only the coolest JavaScript rules hang out. At the door, you see the semicolon (;) bouncer. Every line that enters must end with a semicolon, or it doesn’t get in. Inside, the curly braces { } are dancing, marking the start and end of every block of code. They keep the code organized, so you don’t lose track of your moves.
Key Members of the Syntax Club:
Semicolon (;): Ends a line of code.
Curly Braces { }: Groups code blocks together.
Parentheses ( ): Used for functions, loops, and conditions.
Together, they keep the syntax scene structured! Without these key players, JavaScript gets confused and won’t know what you’re trying to say.
Scene 2: Meet the Operators!
Now let’s meet the Operators – think of them as JavaScript’s toolkit for math, logic, and more. The Operators make it easy to add, compare, and even decide what comes next.
Arithmetic Operators:
+ : Adds values. Like, if you have
let chaiCups = 3 + 2;
– it calculates to 5. More chai, more energy!- : Subtracts.
let energyLevel = 10 - 3;
= 7. (That’s what happens when you skip a snack break.)* : Multiplies. Handy when you want to double or triple things up!
/ : Divides. Who says you can’t share the chai count with friends?
Comparison Operators:
\== : Checks if values are equal. “Is 5 == 5?” Yes! (This one’s more like a friendly handshake – looks at value, not type.)
\=== : Checks if values and types are exactly the same. This one’s serious – both value and type must match!
Fun Example:
Imagine you're deciding what to have for lunch.
If the options are
burger == pizza
(value only), then JS says, “Sure, they’re both food.”But with
burger === pizza
(type + value), JS says, “Nope, they’re totally different!”
Scene 3: Let’s Try It Out!
Try this in your browser’s JavaScript console:
let chaiCups = 2 + 3; // Result: 5
console.log("Total chai cups:", chaiCups);
let energyLevel = 10 - chaiCups;
console.log("Energy after chai:", energyLevel); // Result: 5
console.log(5 == "5"); // true, value is the same
console.log(5 === "5"); // false, type is different
Final Thought: Practice Makes Perfect!
And that’s it for today’s kahani! With syntax and operators, you have the basics to start creating real magic with JavaScript. Next time, we’ll dive into variables and conditions – where the true fun begins. So grab your chai, and let’s keep learning JavaScript, one kahani at a time!
Subscribe to my newsletter
Read articles from Ishant Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Ishant Kumar
Ishant Kumar
Coffee into code, and bugs into pull requests. Always up for debating if TypeScript really makes JavaScript a better person.