101.school
CoursesAbout
Search...⌘K
Generate a course with AI...

    JavaScript 101

    Receive aemail containing the next unit.
    • Introduction to JavaScript
      • 1.1What is JavaScript?
      • 1.2Setting up your environment
      • 1.3Basic syntax and variables
    • Data Types and Variables
      • 2.1Understanding data types
      • 2.2Variables and constants
      • 2.3Operators
    • Control Structures
      • 3.1Conditional statements
      • 3.2Loops
      • 3.3Error handling
    • Functions
      • 4.1Defining functions
      • 4.2Function expressions
      • 4.3Arrow functions
    • Objects and Arrays
      • 5.1Understanding objects
      • 5.2Understanding arrays
      • 5.3Array methods
    • Document Object Model (DOM)
      • 6.1What is the DOM?
      • 6.2Selecting and manipulating elements
      • 6.3Event handling
    • Asynchronous JavaScript
      • 7.1Callbacks
      • 7.2Promises
      • 7.3Async/Await
    • API Interaction
      • 8.1What is an API?
      • 8.2Fetch API
      • 8.3Working with JSON
    • Debugging
      • 9.1Console methods
      • 9.2Debugging tools
      • 9.3Common JavaScript errors
    • JavaScript Libraries
      • 10.1Introduction to libraries
      • 10.2Using jQuery
      • 10.3Using lodash
    • JavaScript Frameworks
      • 11.1Introduction to frameworks
      • 11.2Using React
      • 11.3Using Vue.js
    • Testing and Deployment
      • 12.1Writing tests with Jest
      • 12.2Deployment with Netlify
      • 12.3Continuous integration
    • Final Project
      • 13.1Project proposal
      • 13.2Project development
      • 13.3Project presentation

    Data Types and Variables

    Understanding Variables and Constants in JavaScript

    high-level programming language

    High-level programming language.

    In JavaScript, we use variables and constants to store data that we can use and manipulate in our programs. This article will provide a comprehensive understanding of variables and constants in JavaScript, including how to declare and assign them, the differences between var, let, and const, and the concept of scope and hoisting.

    Variables: Declaration and Assignment

    A variable is a named storage for data. We can declare a variable using the var, let, or const keyword, followed by the name of the variable. For example:

    var name; let age; const pi;

    After declaring a variable, we can assign a value to it using the assignment operator =:

    name = "John"; age = 30; pi = 3.14;

    We can also declare and assign a variable in one line:

    var name = "John"; let age = 30; const pi = 3.14;

    Variable Naming Conventions

    In JavaScript, variable names must start with a letter, underscore (_), or dollar sign ($). They can't start with a number. Variable names are case-sensitive, so name and Name would be two different variables.

    By convention, JavaScript variables are usually written in camelCase, where the first letter of each word except the first is capitalized. For example: myVariableName.

    var, let, and const

    The var keyword was the traditional way to declare variables in JavaScript. However, with the introduction of ES6 (ECMAScript 2015), we now have two new ways to declare variables: let and const.

    The let keyword is similar to var, but it has some important differences. Most notably, let has block scope, which means that a variable declared with let is only accessible within the block it's declared in.

    The const keyword is used to declare constants, which are variables that cannot be reassigned. Once a value is assigned to a const variable, it cannot be changed.

    Scope of Variables: Global vs. Local

    In JavaScript, a variable can have global scope or local scope. A global variable is one that's declared outside of any function or block, and it's accessible from anywhere in the code. A local variable is one that's declared inside a function or block, and it's only accessible within that function or block.

    Hoisting in JavaScript

    Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their containing scope during the compile phase, before the code has been executed. It's important to note that only the declarations are hoisted, not initializations.

    In conclusion, understanding how to work with variables and constants is a fundamental part of programming in JavaScript. By mastering these concepts, you'll be well on your way to becoming a proficient JavaScript developer.

    Test me
    Practical exercise
    Further reading

    Hey there, any questions I can help with?

    Sign in to chat
    Next up: Operators