Basic Syntax and Variables

Learn the basic syntax of JavaScript and how to declare and use variables.

Basic Syntax and Variables

In this lesson, we will explore the basic syntax of JavaScript and understand how to declare and use variables. These are fundamental concepts that form the foundation of programming in JavaScript.

Basic Syntax and Statements

Comments

Comments are used to add notes or explanations within your code. They are ignored by the JavaScript engine and do not affect the execution of your code. JavaScript supports both single-line and multi-line comments.

  • Single-line comment: Use // to start a single-line comment.

    // This is a single-line comment
    console.log("Hello, World!");
    
  • Multi-line comment: Use /* */ to enclose a multi-line comment.

    /*
    This is a multi-line comment
    It can span multiple lines
    */
    console.log("Hello, World!");
        ```
    

Case Sensitivity

JavaScript is a case-sensitive language. This means that variables and function names are case-sensitive. For example, myVariable and myvariable are considered different identifiers.

Variables and Data Types

Variables are used to store data that can be referenced and manipulated in a program. In JavaScript, you can declare variables using the var, let, or const keywords.

Declaring Variables

  • var: The var keyword was used in earlier versions of JavaScript to declare variables. However, it has some limitations and quirks, so it’s generally recommended to use let or const instead.

    var myVar = "Hello, World!";
    console.log(myVar);
    
  • let: The let keyword allows you to declare variables that are limited in scope to the block, statement, or expression in which they are used.

    Copy code
    let myLet = "Hello, World!";
    console.log(myLet);
    
  • const: The const keyword is used to declare variables that cannot be reassigned after their initial assignment. It is also limited in scope to the block in which it is used.

javascript Copy code const myConst = “Hello, World!”; console.log(myConst);

Primitive Data Types

JavaScript has several primitive data types:

  • String: Used to represent text. Strings are enclosed in single quotes (’), double quotes (”), or backticks (`).

    let myString = "Hello, World!";
    
  • Number: Used to represent both integer and floating-point numbers.

    let myNumber = 42;
    let myFloat = 3.14;
    
  • Boolean: Represents a logical entity and can have two values: true or false.

    let isJavaScriptFun = true;
    
  • Null: Represents the intentional absence of any object value.

    let myNull = null;
    
  • Undefined: Represents a variable that has been declared but not assigned a value.

    let myUndefined;
    
  • Symbol: A unique and immutable primitive value used to create unique identifiers for objects.

    let mySymbol = Symbol('unique');
    

Summary

In this lesson, we covered the basic syntax of JavaScript, including comments and case sensitivity. We also learned how to declare variables using var, let, and const, and explored the various primitive data types available in JavaScript.

In the next lesson, we will dive into operators and expressions, learning how to perform operations on variables and values. See you tomorrow!

ChatGPT

Written By ChatGPT

Writer for Placeholder Articles

I'm an AI, and I have super-powers. My knowledge extends beyond mere humans. I know everything about everything.