05. Typy danych - przykłady

Typy danych

Wyświetlanie liczb

<script>
    document.write("<table><tr><th>Wartość dziesiętna<th>Wartość ósemkowa<th>Wartość szesnastkowa<th>Notacja wykładnicza");
    document.write("<tr><td>1: "+ 1 +"<td>01: "+ 01 +"<td>0x1: "+ 0x1 +"<td>0.01e2: "+ 0.01e2);
    document.write("<tr><td>8: "+ 8 +"<td>08: "+ 08 +"<td>0x8: "+ 0x8 +"<td>0.08e2: "+ 0.08e2);
    document.write("<tr><td>123: "+ 123 +"<td>0173: "+ 0173 +"<td>0x78: "+ 0x78 +"<td>1.23e2: "+ 1.23e2);
    document.write("<tr><td>1024: "+ 1024 +"<td>02000: "+ 02000 +"<td>0x400: "+ 0x400 +"<td>1.024e2: "+ 1.024e3);
    document.write("</table>");
</script>

Metody obiektu String

<script>
    var text = "Ala ma kota";
    var _text = "<b>Ala ma kota</b><br />";
    document.write(_text);
    
    document.write("text = \""+ text +"\";<br />");
    document.write("<table><tr><th>Metoda<th>Wywołanie<th>Wynik");
    document.write("<tr><td>length()<td>text.length()<td>" + text.length);
    document.write("<tr><td>charAt()<td>text.charAt(4)<td>" + text.charAt(4));
    document.write("<tr><td>toUpperCase()<td>text.toUpperCase()<td>" + text.toUpperCase());
    document.write("<tr><td>toLowerCase()<td>text.toLowerCase()<td>" + text.toLowerCase());
    document.write("<tr><td>indexOf()<td>text.indexOf('kot')<td>" + text.indexOf('kot'));
    document.write("<tr><td>lastIndexOf()<td>text.lastIndexOf('a')<td>" + text.lastIndexOf('a'));
    document.write("<tr><td>encodeURI()<td>encodeURI(text)<td>" + encodeURI(text));
    document.write("<tr><td>decodeURI()<td>decodeURI('Ala%20ma%20kota')<td>" + decodeURI('Ala%20ma%20kota'));
    document.write("<tr><td>substr()<td>text.substr(7,4)<td>" + text.substr(7,4));
    document.write("<tr><td>slice()<td>text.slice(0,3)<td>" + text.slice(0,3));
    document.write("<tr><td>split()<td>text.split(' ')<td>" + text.split(' '));
    document.write("<tr><td>split()<td>text.split(' ',2)<td>" + text.split(' ',2));
    document.write("<tr><td>replace()<td>text.replace('Ala', 'Ola')<td>" + text.replace('Ala', 'Ola'));
    document.write("</table>");
</script>
        

Typ logiczny

<script>
    document.write("prawda: " + true + "<br />");
    document.write("fałsz: " + false + "<br />");
</script>

Typ obiektowy

<script>
    document.write(document);
</script>

Typ specjalny

<script>
    var test;
    document.write("test: "+ test +"<br />");
    test = null;
    document.write("test: "+ test +"<br />");
</script>

Typ tablicowy

tablica jednowymiarowa
<script>
    var test = [1,2,3,4,5,6,7,8,9,10];
    document.write("test: "+ test +"<br />");
</script>
        
tablica wielowymiarowa
<script>
    var test = [
        [ 1,1,1,1,1,1,1,1 ],
        [ 2,2,2,2,2,2,2,2 ],
        [ 3,3,3,3,3,3,3,3 ],
    ];
    document.write("test: "+ test +"<br />");
</script>