DEV Community

Roja Gnanavel for JSLovers

Posted on

JavaScript: Your First Baby Steps into Syntax & Fundamentals

Ever opened a JavaScript file and wondered, "Why are there so many curly braces and semicolons?"🤯

Welcome to JavaScript!🤓 Before diving into the fun stuff like functions, events, or cool tricks.You first need to understand its basic rules.

Think of syntax as the grammar of JavaScript just like how we have grammar rules in English. If you follow the rules correctly, JavaScript understands what you're saying, and everything works fine.

But if you make a mistake, like missing a bracket or writing something incorrectly, JavaScript won’t understand and will throw an error. Instead of saying "Oops, something’s wrong!", it will give you a real error message, like:

🔴 SyntaxError: Unexpected token (which means JavaScript found something it didn’t expect).

It's JavaScript’s way of saying: "Hey, I don’t understand this 🤷‍♀️"

Does that make sense now?😊

Let’s break it down in a simple and fun way.

1. Statements & Expressions – The Building Blocks

A statement is like a complete instruction in JavaScript

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

Each of these lines is a statement that tells JavaScript what to do.

But what about expressions? 🤔

An expression is a part of a statement that evaluates to a value.

let sum = 5 + 3; 
Enter fullscreen mode Exit fullscreen mode

Here, 5 + 3 is an expression because it evaluates to 8.

So, all expressions are part of a statement, but not all statements are expressions. Cool, right? 😎


2. Comments – Because Your Brain Won’t Remember Everything 🤯

Comments are like notes in your code.They make your code easier to understand for you and anyone reading it later. Trust me, you’ll thank yourself for using them.

🔹 Single line comments

// This is a single line comment
let age = 29;
Enter fullscreen mode Exit fullscreen mode

🔹 Multi line comments

/*
  This is a multi line comment.
  You can explain logic here.
*/
let isDeveloper = true;
Enter fullscreen mode Exit fullscreen mode

Use them.Future you will thank you.😎


3. JavaScript is Case Sensitive – Be Careful ⚠️

JavaScript treats uppercase and lowercase differently.So, "Name" and "name" are not the same.😱

let Name = "Roja";  
let name = "Another Roja";  

console.log(Name); // Roja  
console.log(name); // Another Roja 
Enter fullscreen mode Exit fullscreen mode

4. Variables – The Right Way to Store Data

JavaScript gives us three ways to declare variables. Let’s break them down:

🔹 var - The old school one – Avoid It

var city = "Chennai";
Enter fullscreen mode Exit fullscreen mode

It works, but has weird scoping issues. Nobody uses var anymore.So, just skip it.

🔹 let - The go to choice

let country = "India";
Enter fullscreen mode Exit fullscreen mode

Use let when the value might change later.

🔹 const - The unchangeable one

const PI = 3.14159;
Enter fullscreen mode Exit fullscreen mode

Use const when the value should never change.


5. Hoisting – JavaScript’s Magic Trick✨

JavaScript moves variable and function declarations to the top before running the code.

console.log(name); // undefined
var name = "Roja";
Enter fullscreen mode Exit fullscreen mode

With var, it’s hoisted but not initialized, so you get undefined.

🚨 But let and const don’t work the same way!

console.log(age); 
let age = 29; 
// ❌ ReferenceError: Cannot access 'age' before initialization
Enter fullscreen mode Exit fullscreen mode

So always declare variables before using them!


6. Strict Mode – No More Silly Mistakes 🚫

"use strict" is a special directive in JavaScript that helps you write cleaner and more secure code. When you add it at the top of your script or function, it enables Strict Mode, which changes how JavaScript behaves in some important ways.

Why use it?
Strict mode helps you,

  • Catch common coding mistakes
  • Prevent the use of undeclared variables
  • Make your code more predictable and safer

Example: Without strict mode

x = 10; // No error, but x is not declared properly
console.log(x);

Enter fullscreen mode Exit fullscreen mode

Without "use strict", JavaScript would allow this mistake.

Example: With strict mode

"use strict";
x = 10; // Error! x is not declared
Enter fullscreen mode Exit fullscreen mode

Where to use it?

  • At the top of a file
"use strict";
// your code here
Enter fullscreen mode Exit fullscreen mode
  • Or inside a function
function test() {
  "use strict";
  // strict mode only inside this function
}

Enter fullscreen mode Exit fullscreen mode

That’s it. It’s just one line, but it helps avoid silly mistakes.


7. Template Literals

you can join strings using either the + operator or template literals (also called template strings). However, template literals offer cleaner, more readable, and maintainable code.

🔹 Using Template Literals:

// Uses backticks (`) instead of regular quotes ("" or '')
// Variables and expressions are wrapped in ${}

let name = "Roja";
console.log(`Hello, ${name}!`); // Hello, Roja!
Enter fullscreen mode Exit fullscreen mode

8. Destructuring – Extracting Values Like a Pro🔥

Instead of this old school way:

const user = { name: "Roja", age: 29 };
const userName = user.name;
const userAge = user.age;
Enter fullscreen mode Exit fullscreen mode

Use destructuring

const { name, age } = user;
console.log(name, age); // Roja 29
Enter fullscreen mode Exit fullscreen mode

Works for arrays too:

const colors = ["red", "blue", "green"];
const [firstColor, secondColor] = colors;
console.log(firstColor, secondColor); // red blue
Enter fullscreen mode Exit fullscreen mode

Less code, more clarity.


9. Spread & Rest Operators (...) – JavaScript’s Magic Trick ✨

🔹 Spread Operator – Expanding Arrays
Want to quickly copy or merge arrays? Use the spread operator.

const arr1 = [1, 2, 3];
const arr2 = [...arr1, 4, 5];
console.log(arr2); // [1, 2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

🔹 Rest Operator – Collecting Multiple Values
Need a function to accept multiple arguments? The rest operator has your back!

function sum(...numbers) {
  return numbers.reduce((total, num) => total + num, 0);
}
console.log(sum(1, 2, 3, 4)); // 10
Enter fullscreen mode Exit fullscreen mode

One spreads, the other gathers both make your code cleaner.


10. Control Flow – Making JavaScript Decide for You 🤔

🔹 If-Else Statement – Making simple decisions

let temperature = 30;
if (temperature > 25) {
  console.log("It's hot outside.");
} else {
  console.log("It's cool today.");
}
Enter fullscreen mode Exit fullscreen mode

🔹 Switch Statement – Handling multiple cases

let day = "Monday";
switch (day) {
  case "Monday":
    console.log("Ugh, Monday blues.");
    break;
  case "Friday":
    console.log("Yay! Weekend is near.");
    break;
  default:
    console.log("Just another day.");
}
Enter fullscreen mode Exit fullscreen mode

11. Loops – Running Code Again & Again

Loops help us execute the same block of code multiple times without repeating ourselves.

🔹 For Loop – Repeat something a fixed number of times

for (let i = 1; i <= 5; i++) {
  console.log("Number:", i);
}
Enter fullscreen mode Exit fullscreen mode

Runs 5 times, increasing i each time.

🔹 While Loop – Repeat while a condition is true

let count = 1;
while (count <= 3) {
  console.log("Counting:", count);
  count++;
}

Enter fullscreen mode Exit fullscreen mode

Runs as long as count <= 3.

🔹 Do-While Loop – Always runs at least once

let num = 5;
do {
  console.log("This runs at least once.");
  num++;
} while (num <= 3);
Enter fullscreen mode Exit fullscreen mode

Runs once, even though the condition is false at the start.

🔹 For-of Loop – Loop through arrays

const fruits = ["Apple", "Banana", "Cherry"];
for (const fruit of fruits) {
  console.log(fruit);
}
Enter fullscreen mode Exit fullscreen mode

Outputs each fruit one by one.

🔹 For-in Loop – Loop through object properties

const user = { name: "Roja", age: 29 };
for (const key in user) {
  console.log(key, ":", user[key]);
}
Enter fullscreen mode Exit fullscreen mode

Loops through keys in an object (name, age).


12. Functions – Write Once, Use Anytime

🔹 Function Declaration

function greet(name) {
  console.log("Hello, " + name + "!");
}
greet("Roja"); // Hello, Roja!
Enter fullscreen mode Exit fullscreen mode

🔹 Arrow Function – The modern, shorter way

const add = (a, b) => a + b;
console.log(add(5, 3)); // 8
Enter fullscreen mode Exit fullscreen mode

Conclusion

Great job. You’ve taken your first baby steps into JavaScript by learning its basic rules and structure. Now you understand how statements, variables, loops, and functions work.

But learning JavaScript takes time! The more you practice and explore, the better you’ll get.

So, keep going, have fun, and enjoy your JavaScript journey.

Top comments (0)