JavaScript Syntax

JavaScript is a programming language that is used to create interactive and dynamic web pages. Here are some key syntax rules to keep in mind when writing JavaScript code:

  1. Comments: To add comments in JavaScript, use the “//” symbol for a single-line comment, or use “/* */” for a multi-line comment.
  2. Variables: Use the “var”, “let”, or “const” keyword to declare variables. For example:
  3. var x = 10; let y = "hello"; const z = true;
  4. Data Types: JavaScript supports several data types, including numbers, strings, booleans, null, and undefined.
  5. Operators: Use operators such as “+” for addition, “-” for subtraction, “*” for multiplication, “/” for division, and “%” for modulus.
  6. Conditionals: Use conditional statements such as “if”, “else if”, and “else” to execute code based on certain conditions.
  7. Loops: Use loops such as “for”, “while”, and “do-while” to execute code repeatedly.
  8. Functions: Use the “function” keyword to define a function. For example:
  9. function addNumbers(x, y) { return x + y; }
  10. Objects: Use objects to store data as key-value pairs. For example:
  11. evar person = { name: "John", age: 30, isMarried: false };
  12. Arrays: Use arrays to store multiple values in a single variable. For example:
  13. var fruits = ["apple", "banana", "orange"];
  14. DOM manipulation: Use JavaScript to manipulate the Document Object Model (DOM) of a web page to dynamically change the content and style of elements on the page. For example:
document.getElementById("myButton").addEventListener("click", function() {
    document.getElementById("myDiv").innerHTML = "Hello, world!";
    document.getElementById("myDiv").style.backgroundColor = "blue";
});

You may also like...

Popular Posts

Leave a Reply

Your email address will not be published. Required fields are marked *