Ćwiczenia - Typy Danych JavaScript

Każde ćwiczenie zawiera krótką instrukcję. Kliknij "Pokaż rozwiązanie", aby zobaczyć kod.

Ćwiczenie 1: Sprawdź typ danych

function checkType() {
    let value = document.getElementById("input1").value;
    document.getElementById("wynik1").innerHTML = typeof value;
}

Ćwiczenie 2: Konwersja do liczby

function convertToNumber() {
    let value = document.getElementById("input2").value;
    let number = Number(value);
    document.getElementById("wynik2").innerHTML =
        number + " (typ: " + typeof number + ")";
}

Ćwiczenie 3: Konwersja do stringa

function convertToString() {
    let num = 42;
    let text = String(num);
    document.getElementById("wynik3").innerHTML =
        text + " (typ: " + typeof text + ")";
}

Ćwiczenie 4: Konwersja do boolean

function convertToBoolean() {
    let value = document.getElementById("input4").value;
    let boolValue = Boolean(value);
    document.getElementById("wynik4").innerHTML =
        boolValue + " (typ: " + typeof boolValue + ")";
}

Ćwiczenie 5: Sprawdzenie wartości null

Uwaga: typeof null zwraca "object".

function checkNull() {
    let value = null;
    document.getElementById("wynik5").innerHTML = (value === null);
}

Ćwiczenie 6: Sprawdzenie wartości undefined

function checkUndefined() {
    let value;
    document.getElementById("wynik6").innerHTML = (value === undefined);
}

Ćwiczenie 7: Sprawdzenie czy wartość jest liczbą

function checkIfNumber() {
    let value = document.getElementById("input7").value;
    let num = Number(value);
    let isNumber = !Number.isNaN(num);
    document.getElementById("wynik7").innerHTML =
        isNumber + " (po konwersji: " + num + ")";
}

Ćwiczenie 8: Sprawdzenie czy wartość to obiekt (z JSON)

function checkIfObject() {
    let raw = document.getElementById("input8").value;
    try {
        let value = JSON.parse(raw);
        let isPlainObject =
            typeof value === "object" &&
            value !== null &&
            !Array.isArray(value);
        document.getElementById("wynik8").innerHTML =
            isPlainObject + " (typ: " + typeof value + ")";
    } catch (e) {
        document.getElementById("wynik8").innerHTML = "Niepoprawny JSON";
    }
}

Ćwiczenie 9: Sprawdzenie czy wartość to tablica (z JSON)

function checkIfArray() {
    let raw = document.getElementById("input9").value;
    try {
        let value = JSON.parse(raw);
        let isArray = Array.isArray(value);
        document.getElementById("wynik9").innerHTML =
            isArray + " (typ: " + typeof value + ")";
    } catch (e) {
        document.getElementById("wynik9").innerHTML = "Niepoprawny JSON";
    }
}

Ćwiczenie 10: Sprawdzenie długości stringa

function checkStringLength() {
    let text = document.getElementById("input10").value;
    document.getElementById("wynik10").innerHTML = "Długość: " + text.length;
}

Ćwiczenie 11: Typ symbol

function checkSymbol() {
    let desc = document.getElementById("input11").value || "id";
    let s = Symbol(desc);
    document.getElementById("wynik11").innerHTML =
        String(s) + " (typ: " + typeof s + ")";
}

Ćwiczenie 12: Typ bigint

function checkBigint() {
    let raw = document.getElementById("input12").value;
    try {
        let big = BigInt(raw);
        document.getElementById("wynik12").innerHTML =
            big + " (typ: " + typeof big + ")";
    } catch (e) {
        document.getElementById("wynik12").innerHTML =
            "Nie można skonwertować na BigInt";
    }
}

Ćwiczenie 13: Funkcja jako typ

function checkFunctionType() {
    function greet(name) {
        return "Cześć, " + name + "!";
    }
    document.getElementById("wynik13").innerHTML =
        "typeof greet: " + (typeof greet) +
        ", wynik greet('Jan'): " + greet("Jan");
}