JavaScript Syntax and Examples

1. Basic Structure of JavaScript

JavaScript code is usually placed inside <script> tags in an HTML file. Example:

<script>
  console.log("Welcome to AIT!");
</script>
                    

Feedback: This is the basic JavaScript structure that is essential for every website!

2. Printing Messages

You can print messages using console.log(). Example:

console.log("Hello, welcome to aitaurangabad.com!");
                    

Feedback: Printing messages in the console is very helpful for debugging.

3. Variables: Storing Information

Variables store values like numbers or strings. Example:

let age = 21; // Storing age
let name = "Zaid"; // Storing name
                    

Feedback: By using variables, we can manage data

4. Types of Data

JavaScript has multiple data types such as numbers, strings, and booleans. Example:

let isStudent = true; // Boolean value
let score = 95; // Number
let greeting = "Hello!"; // String
                    

Feedback: By using different data types, we represent specific types of values.

5. Using Operators

Operators help you perform operations like addition or comparison. Example:

let sum = 10 + 5; // Adds 10 and 5
let isEqual = (10 == 10); // Checks if 10 equals 10 (true)
                    

Feedback: By using operators, we can perform calculations and make comparisons.

6. Creating Expressions

Expressions combine values and operators to get a result. Example:

let total = (5 + 3) * 2; // Adds 5 and 3, then multiplies by 2 (result = 16)
                    

Feedback: By performing operations in expressions, we generate dynamic values.

7. Using Functions

Functions are reusable blocks of code. Example:

function greet(name) {
    return "Hello, " + name + "!";
}
console.log(greet("Daniya")); // Calls the function
                    

Feedback: By using functions, we organize and make our code reusable.

8. Comments

Comments are used to explain code. Example:

// This is a single-line comment
/* This is a multi-line comment */
                    

Feedback: Comments make the code easier to understand and help other developers.