Home >Web Front-end >JS Tutorial >Day Variables and Data Types in JavaScript

Day Variables and Data Types in JavaScript

Barbara Streisand
Barbara StreisandOriginal
2024-12-10 20:10:20427browse

Day Variables and Data Types in JavaScript

Day 2: Variables and Data Types in JavaScript

Welcome to Day 2 of learning JavaScript! Today, we’ll explore the building blocks of any program: variables and data types. These concepts are crucial as they form the foundation for everything you do in JavaScript.


What Are Variables?

A variable is like a container that holds data values. Think of it as a labeled box where you can store information, retrieve it later, or even change its contents.

Declaring Variables in JavaScript

JavaScript provides three ways to declare variables:

  1. var - The old way (avoid using this unless necessary).
  2. let - Recommended for variables that can change.
  3. const - For variables that shouldn’t change (constants).

Example:

var oldWay = "Avoid this if possible";
let currentWay = "Use let for variables that can change";
const fixedValue = "Use const for constants";

Difference Between var, let, and const

Feature var let const
Scope Function-scoped Block-scoped Block-scoped
Reassignable Yes Yes No
Redeclarable Yes No No
Feature
var let const
Scope Function-scoped Block-scoped Block-scoped
Reassignable Yes Yes No
Redeclarable Yes No No

Example:

var oldWay = "Avoid this if possible";
let currentWay = "Use let for variables that can change";
const fixedValue = "Use const for constants";

JavaScript Data Types

JavaScript has two types of data: Primitive and Non-Primitive.

Primitive Data Types

  1. String: Textual data. Example:
function scopeTest() {
    if (true) {
        var x = "Function scope";
        let y = "Block scope";
        const z = "Constant";
    }
    console.log(x); // Accessible
    // console.log(y); // Error: y is not defined
    // console.log(z); // Error: z is not defined
}
scopeTest();
  1. Number: Numeric data. Example:
   let name = "Arjun";
   console.log(name); // "Arjun"
  1. Boolean: True or false values. Example:
   let age = 22;
   console.log(age); // 22
  1. Null: Represents an intentional absence of value. Example:
   let isStart_up_guy = true;
   console.log(isStart_up_guy); // true
  1. Undefined: A variable that has been declared but not assigned a value. Example:
   let emptyValue = null;
   console.log(emptyValue); // null
  1. Symbol: A unique and immutable value. Example:
   let uninitialized;
   console.log(uninitialized); // undefined

Type Conversion

JavaScript allows you to convert values between types.

Implicit Conversion (Coercion)

JavaScript sometimes converts types automatically.

Example:

   let uniqueKey = Symbol("key");
   console.log(uniqueKey); // Symbol(key)

Explicit Conversion

You can manually convert types using built-in functions like Number(), String(), or Boolean().

Example:

let result = "5" + 5; // String + Number
console.log(result); // "55" (string)

Practice for Today

  1. Declare variables using let and const to store:

    • Your favorite book name.
    • The number of books you own.
    • Whether you like reading.
  2. Try type conversion:

    • Convert a number to a string.
    • Convert a string to a number.

Summary of Day 2

Today, we covered:

  1. Variables: var, let, and const.
  2. Primitive Data Types: String, Number, Boolean, Null, Undefined, and Symbol.
  3. Type Conversion: Implicit and explicit ways to convert types in JavaScript.

Next Steps

Tomorrow, we’ll dive into operators and expressions in JavaScript to start manipulating data and writing more complex programs. Stay tuned for Day 3: Operators and Expressions!

The above is the detailed content of Day Variables and Data Types in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn