30 Days of JavaScript: JavaScript Syntax and Core Concepts — Day 2

30 Days of Javascript Series by DopeThemes

Welcome back, budding JavaScript enthusiasts! As we embark on Day 2 of our exciting journey through the world of JavaScript, we’ll delve into the fundamental building blocks that make up the language: variables, data types, operators, conditional statements, loops, functions, and scope. By grasping these core concepts, you’ll pave the way for a deeper understanding of JavaScript.

A Closer Look at Variables, Data Types, and Operators

Variables

In JavaScript, variables are the workhorses that store and manipulate data. To declare a variable, use the let or const keyword, followed by the variable name:

let ourVariable;
const ourConstant = 42;

While let permits you to modify a variable’s value, const locks it in place, creating an unchangeable constant.

Data Types

At the heart of JavaScript lie a handful of fundamental data types:

string: Holds textual data.

let str = "Hello, world!";

number: Accommodates both integers and floating-point numbers.

let float = 3.14;

boolean: Conveys true or false values.

let isJavaScriptFun = true;

null: Represents an empty or non-existent value.

let emptyValue = null;

undefined: Designates a variable that hasn’t been assigned a value.

let uninitializedVar;

object: Contains complex data structures.

let person = {
    firstName: "John",
    lastName: "Doe",
    age: 30
};

symbol: Encapsulates a unique, immutable value.

let uniqueID = Symbol("id");

Operators

Operators perform operations on variables and values, and some prevalent ones include:

Arithmetic operators: +, -, *, /, %, **

Arithmetic operators are like the simple calculations you perform in everyday life, but in the context of programming. Let’s break them down:

+: The addition operator is like when you combine two blog posts’ word counts to find the total number of words. It’s just like adding two numbers.

-: Subtraction is the process of taking one value away from another. For example, if you have 100 visitors today and 80 visitors yesterday, you’d use subtraction to find the difference: 100 – 80 = 20.

*: Multiplication can help you scale values, like when you’re calculating the revenue of selling multiple WordPress themes. If each theme is priced at $50 and you sell 10, you’d multiply them: 50 * 10 = $500.

/: Division is about splitting values into equal parts. Imagine you have a total of 500 website views and want to know how many views you have on average over five days. Divide 500 by 5 to get the answer: 500 / 5 = 100.

%: The modulus operator is like sharing cookies equally among friends and finding out how many are left over. If you have 11 cookies and four friends, each would get two cookies, and three would be left: 11 % 4 = 3.

**: Exponentiation is all about raising numbers to a certain power. For instance, if you want to calculate the growth of your website traffic after doubling it for three consecutive months, you’d use the exponentiation operator: 2 ** 3 = 8. This means your traffic increased eightfold.

Comparison operators: ==, ===, !=, !==, <, >, <=, >=

Comparison operators are used in programming to compare two values, determining their relationship (equal, not equal, less than, greater than, etc.). They help in making decisions and controlling the flow of your code based on the results of these comparisons.

==: Equal to. Picture your site offering a members-only area, and you need to check if a user’s input matches the correct password. The ‘equal to’ operator compares the two values, returning true if they match and false if they don’t.

===: Strict equal to. This operator not only checks if the values match but also ensures they’re of the same data type. It’s like comparing apples to apples instead of apples to oranges, ensuring you’re working with consistent data in your code.

!=: Not equal to. Imagine you’re filtering out spam comments on your blog by checking if a comment’s content doesn’t match a certain spammy keyword. The ‘not equal to’ operator returns true if the values don’t match, helping you keep your comments section clean and relevant.

!==: Strict not equal to. This operator checks both the value and the data type, ensuring they don’t match. It’s like making sure you’re not comparing two completely different things, such as a string with a number.

<, >, <=, >=: These operators compare values by size or order, like when you’re ranking blog posts based on the number of views. They help you find out if one value is less than (<), greater than (>), less than or equal to (<=), or greater than or equal to (>=) another value

Logical operators: &&, ||, !

Logical operators are used to combine or modify the outcomes of comparison operations or conditions. They work with boolean values (true or false) to create more complex logic in your code, using AND, OR, and NOT operations.

&&: The logical AND operator checks if both conditions are true. Imagine you’re creating a custom form on your site, and you want to verify if a user has both entered their email address and agreed to your terms of service. The AND operator helps ensure both conditions are met.

||: The logical OR operator checks if at least one of the conditions is true. Think about offering a promotion on your site where users can get a discount if they either share your post on social media or subscribe to your newsletter. The OR operator allows you to check if either condition is met to grant the discount.

!: The logical NOT operator negates a condition, flipping its truthiness. For instance, if you want to hide an admin panel from users who aren’t administrators, you’d use the NOT operator to invert their admin status and hide the panel when the condition is false.

Mastering Conditional Statements and Loops

Conditional Statements

Conditional statements grant you the power to execute specific code blocks based on varying conditions. The most frequently used conditional statement is the if…else statement:

if (condition) {
  // Code executed if the condition is true
} else {
  // Code executed if the condition is false
}

Loops

Loops enable you to run a code block repeatedly. Two widely used loops are for and while:

for (let i = 0; i < 10; i++) {
  // Code executed during each iteration
}

while (condition) {
  // Code executed as long as the condition remains true
}

Delving into Functions and Scope

Functions

Functions are modular code blocks that can be called with a designated set of arguments. To declare a function, use the function keyword:

function ourFunction(arg1, arg2) {
  // Code executed
  return result;
}

Scope

Scope pertains to the visibility and lifespan of variables in your code. JavaScript features two primary scope types:

  • Global scope: Variables declared outside any function or code block are accessible throughout the entire program.
  • Local scope: Variables declared inside a function or code block are only accessible within that specific context.

A firm grasp of scope is essential for managing variables and sidestepping conflicts in your code.

Conclusion

Today, we’ve delved deeper into the fundamental concepts of JavaScript: variables, data types, operators, conditional statements, loops, functions, and scope. By internalizing these core elements, you’re laying the foundation for mastering this powerful language.

Understanding these concepts is not just about knowing the syntax; it’s about embracing the philosophy of programming. This foundation will empower you to explore more complex structures and patterns in the coming days.

In the upcoming days, we’ll immerse ourselves in more advanced topics, such as objects, arrays, and events, specifically focusing on arrays and objects in JavaScript on Day 3. Each day will provide you with unique insights and challenges that will both stretch and solidify your knowledge.

Stay tuned and nurture that coding enthusiasm! This journey is about growth, creativity, and the joy of building something meaningful. Keep your curiosity alive and get ready for a thrilling adventure through the world of code.

Next: 30 Days of JavaScript: In-Depth Exploration of Arrays and Objects in JavaScript — Day 3


Stay in the loop with our web development newsletter - no spam, just juicy updates! Join us now. Join our web development newsletter to stay updated on the latest industry news, trends, and tips. No spam, just juicy updates delivered straight to your inbox. Don't miss out - sign up now!


We’ve tried our best to explain everything thoroughly, even though there’s so much information out there. If you found our writing helpful, we’d really appreciate it if you could buy us a coffee as a token of support.

Also, if you’re interested in learning more about WordPress, Javascript, HTML, CSS, and programming in general, you can subscribe to our MailChimp for some extra insights.

Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.