JS Syntax

JavaScript Syntax

JavaScript syntax refers to the set of rules that define the structure of JavaScript code. Just like grammar in a language, syntax in JavaScript ensures that the code is written in a way that the computer can understand and execute.
Here’s a breakdown of the essential elements of JavaScript syntax:

1. Basic Structure of JavaScript

To use JavaScript on a web page, you place your code inside <script> tags.
Here’s how it looks:

<script>
  // Your JavaScript code goes here
  console.log(“Hello, World!”);
</script>

2. Printing Messages

You can use console.log() to display messages or results in the console (a special area in your web browser).
For example:

console.log(“Hello, World!”); // This prints “Hello, World!” in the console

3. Variables: Storing Information

Variables are like boxes where you can store information. You can create a variable using let or const.
Using let:

let age; // Declares a variable named age
age = 15; // Assigns the value 15 to age

// You can also declare and assign at the same time
let name = “John”; // Declares a variable named name and assigns “John”

4. Types of Data

JavaScript can store different types of data:
• Numbers: For example, ages or prices.

  let score = 95; // A whole number

  let price = 9.99; // A decimal number

• Strings: Text inside quotes.

  let greeting = “Hello!”; // A string

• Booleans: True or false values.

  let isStudent = true; // This means yes, the person is a student

5. Using Operators

Operators let you perform operations on values.
• Arithmetic Operators: For math calculations.

let sum = 5 + 3; // Adds 5 and 3, result is 8
let difference = 10 – 4; // Subtracts 4 from 10, result is 6

• Assignment Operators: Used to assign values.

let x = 10; // Assigns 10 to x
x += 5; // Now x is 15 (10 + 5)

6. Creating Expressions

An expression combines values and operators to give a result.

let result = (4 + 6) * 2; // Adds 4 and 6 to get 10, then multiplies by 2, the result is 20

7. Important Keywords

Keywords are special words in JavaScript that have specific meanings.
Here are some examples:
• let and const: Used to declare variables.
• function: Used to create a function.

8. Comments: Writing Notes in Code

Comments help you write notes in your code. They don’t affect how the code runs.
• Single-line comments use //:

  // This is a single-line comment

• Multi-line comments use /* … */:

/*
This is a
multi-line comment
*/

9. Functions: Blocks of Code

Functions are like mini-programs that do a specific task. You can create a function and call it when needed.

function sayHello() {
console.log(“Hello, everyone!”);
}

sayHello(); // Calls the function to print “Hello, everyone!”

10. Identifiers: Naming Variables

Identifiers are names you give to variables and functions. They must start with a letter, underscore (_), or dollar sign ($).
• Valid identifiers:

let firstName; // Valid
let _age; // Valid
let $price; // Valid

11. Case Sensitivity

JavaScript is case-sensitive, meaning that it treats uppercase and lowercase letters differently.
For example:

let name = “Alice”; // This is a valid variable
let Name = “Bob”; // This is also a valid variable, but it’s different from ‘name’

// Using them
console.log(name); // Outputs: Alice
console.log(Name); // Outputs: Bob

So, name and Name are considered two different variables!

12. Camel Case Naming

In JavaScript, it’s common to use a camel case for naming variables, which means starting the second word with a capital letter.

let firstName = “Alice”; // camel case
let lastName = “Smith”; // camel case

Conclusion

JavaScript syntax is the set of rules for writing code. By following these rules, you can create amazing interactive websites. Practice writing simple JavaScript programs to get the hang of it!

Course Video in Hindi

Course Video in English
YouTube Reference :