DEV Community

Cover image for If You Struggle With JavaScript Syntax, This Guide Is for You
Mene.tech
Mene.tech

Posted on

If You Struggle With JavaScript Syntax, This Guide Is for You

🧠 What is "syntax" in JavaScript?

In plain terms, syntax is the set of rules that tells JavaScript how to interpret what you write.

It’s like grammar in a language. If you say:

“Go store to I the went.”
The words are correct, but the order breaks the rules of English. Same thing happens when JavaScript sees something like:

let = 5 number 
Enter fullscreen mode Exit fullscreen mode

💥 SyntaxError: Unexpected token '='

🟢 Let's Break It All Down

1. 🔸 Variable Declaration

What it’s doing:

  • let → a keyword that tells JavaScript: “Hey, I’m about to define a variable.”

  • age → the name of the variable (also called an identifier)

  • = → the assignment operator. This means “store the value on the right inside the variable on the left.”

  • 19 → a number value (called a primitive)

  • ; → the semicolon ends the instruction (optional in JS, but recommended)

So this line means:

➡️ “Create a new variable called age, and set it to the number 19.”

2. 🔸 Strings

let name = "Mene"; 
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • "Mene" is in quotes, so JavaScript treats it as text, not a variable or number.

  • You can use either " or ' or backticks for template strings.

Why the quotes matter:

let name = Mene; // ❌ JS thinks Mene is a variable, not a string. 
Enter fullscreen mode Exit fullscreen mode

3. 🔸 Functions

function greet(name) { return "Hello, " + name; } 
Enter fullscreen mode Exit fullscreen mode

Line by line:

  • function → a keyword that says, “I’m defining a function now.”
  • greet → the name of the function.
  • (name) → this is a parameter. It’s a placeholder for any value passed into the function.
  • {} → the function body. All logic for this function must live inside here.
  • return → tells the function what value to give back when it's called.
  • "Hello, " + name → concatenates a string and the value of the variable name.

How it works:

greet("Mene"); // ➡️ returns "Hello, Mene" 
Enter fullscreen mode Exit fullscreen mode

4. 🔸 Conditionals (if, else)

if (age >= 18) { console.log("You're an adult."); } else { console.log("You're still young."); } 
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • if → keyword to start a conditional.
  • (age >= 18) → the condition being tested. If this is true, the next block runs.

    • >= is a comparison operator meaning "greater than or equal to".
  • {} → contains the block of code to run if the condition is true.

  • else → runs if the if condition fails.


5. 🔸 Loops

for (let i = 0; i < 5; i++) { console.log(i); } 
Enter fullscreen mode Exit fullscreen mode

What’s happening here:

Part Meaning
for loop keyword
let i = 0 start the counter at 0
i < 5 loop continues while this is true
i++ after every loop, increase i by 1
console.log(i); print the current value of i

This prints:

0 1 2 3 4 
Enter fullscreen mode Exit fullscreen mode

Why not 5? Because once i hits 5, i < 5 becomes false.


6. 🔸 Arrays

let colors = ["red", "green", "blue"]; 
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Square brackets [] mean you’re creating an array — a list.
  • Values are separated by commas.
  • You can access them with index numbers:
 colors[0] // "red" 
Enter fullscreen mode Exit fullscreen mode

Why colors[0] and not colors[1]?
Because arrays are zero-indexed in JS — counting starts from 0.


7. 🔸 Objects

let user = { name: "Mene", age: 19, isWriter: true }; 
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Curly braces {} create an object.
  • Inside, you have key: value pairs.

    • name is the key → "Mene" is the value.
  • You access properties like this:

 user.name // "Mene" 
Enter fullscreen mode Exit fullscreen mode

Why use objects?
Because they model real-world things: a user, a product, a post — with properties.


8. 🔸 Function Expression vs Declaration

// Function declaration function sayHi() { return "Hi!"; } // Function expression const sayHi = function() { return "Hi!"; }; 
Enter fullscreen mode Exit fullscreen mode

The difference?
Function declarations get hoisted (moved to the top during runtime), so you can call them before they're defined.

Function expressions don’t, they're stored like variables.


9. 🔸 Arrow Functions

const greet = (name) => { return `Hi, ${name}`; }; 
Enter fullscreen mode Exit fullscreen mode
  • This is just a shorter way to write a function.
  • Template string (with backticks) allows us to embed variables using ${}

10. 🔸 Comments

// This is a single-line comment /* This is a multi-line comment */ 
Enter fullscreen mode Exit fullscreen mode

Use comments to explain your code to other humans or future you.


✅ TL;DR: JavaScript Syntax Cheat Sheet (With Meaning)

Syntax What it means
let, const Create variables
function Define a function
() Function parameters or invocation
{} Block of code (scopes, functions, conditionals)
[] Array
: Key-value in objects
; End a statement (optional, but cleaner)
// Comment

🧩 Interactive Exercises: JavaScript Syntax


1. 🔢 Fix the Variable Declaration

🧪 Challenge: This variable declaration has a bug. Can you fix it?

let 1stName = "Ihuoma"; 
Enter fullscreen mode Exit fullscreen mode

❓ What’s wrong?

Variables can’t start with a number. Rewrite it correctly.

✅ Answer

let firstName = "Ihuoma"; 
Enter fullscreen mode Exit fullscreen mode

2. Predict the Output (Strings & Variables)

🧪 Challenge: What will this code print?

let name = "Mene"; console.log("Hello name"); 
Enter fullscreen mode Exit fullscreen mode

✅ Answer

It prints:

Hello name 
Enter fullscreen mode Exit fullscreen mode

Not "Hello Mene" because it's a string, not a variable reference.

To fix it:

console.log("Hello " + name); 
Enter fullscreen mode Exit fullscreen mode

Or with template literals:

console.log(`Hello ${name}`); 
Enter fullscreen mode Exit fullscreen mode

3. Write Your First Function

🧪 Challenge: Complete the function to return a message like "Welcome, Ada!"

function welcome(___) { return "Welcome, " + ___; } 
Enter fullscreen mode Exit fullscreen mode

Then call it with your name and print the result.

✅ Answer

function welcome(name) { return "Welcome, " + name; } console.log(welcome("Ada")); 
Enter fullscreen mode Exit fullscreen mode

4. Fix the Loop

🧪 Challenge: There’s something wrong with this loop. It goes on forever! Why?

let i = 0; while (i < 3) { console.log(i); } 
Enter fullscreen mode Exit fullscreen mode

✅ Answer

There's no i++ inside the loop the condition i &lt; 3 is always true.

Corrected:

let i = 0; while (i &lt; 3) { console.log(i); i++; } 
Enter fullscreen mode Exit fullscreen mode

5. Access the Array

🧪 Challenge: What will this print?

let books = ["JS Basics", "CSS Magic", "React Guide"]; console.log(books[3]); 
Enter fullscreen mode Exit fullscreen mode

✅ Answer

It prints:

undefined 
Enter fullscreen mode Exit fullscreen mode

Because there is no books[3]. Indexing starts at 0, so the last item is books[2].


6. Spot the Syntax Error

🧪 Challenge: This object has a problem. What’s wrong?

let user = { name: "Mene" age: 19, isOnline: true }; 
Enter fullscreen mode Exit fullscreen mode

✅ Answer

You’re missing a comma between properties.

Fixed:

let user = { name: "Mene", age: 19, isOnline: true }; 
Enter fullscreen mode Exit fullscreen mode

7. Arrow Function Conversion

🧪 Challenge: Convert this function into an arrow function:

function greet(name) { return "Hi " + name; } 
Enter fullscreen mode Exit fullscreen mode

✅ Answer

const greet = (name) =&gt; { return "Hi " + name; }; 
Enter fullscreen mode Exit fullscreen mode

Or shorter:

const greet = name =&gt; "Hi " + name; 
Enter fullscreen mode Exit fullscreen mode

I hope this guide helped you move from just writing code to truly understanding it. You can drop your comments and critics!👀😊

Top comments (0)