js learning road 3: data types

Posted by genetheblue on Sun, 01 Dec 2019 13:20:45 +0100

1. String:

<!DOCTYPE html>
<html>
<body>

<script>
var apple = "Apple";
var banana = "Banana";
var orange = "A mandarin orange";
var name1 = "Xiao Ming";
var name2 = "Xiaohong";

document.write(name1 + "Say: teacher, I want to eat" + apple + "<br>");
document.write(name2 + "Say: teacher, I want to eat" + banana + "and" + orange);
</script>

</body>
</html>

2. numerical value

js, the numeric type is relatively simple, and does not distinguish integer floating-point and other numeric types. This is the number, with or without decimal point, which is the same for js.

<!DOCTYPE html>
<html>
<body>

<script>
var a = 1;
var b = 2.3;
var c = a + b;
var name1 = "Xiao Ming";
var name2 = "Xiaohong";

document.write(name1 + "ask" + name2 + ": " + a + "+" + b + "How much is it?" + "<br>");
document.write(name2 + "The answer is:" + a + "+" + b + "Be equal to" + c + "ah!" + "<br>");

</script>

</body>
</html>

In js, can strings and values be added directly for splicing?

Experiment:

<!DOCTYPE html>
<html>
<body>

<script>
var a = 1;
var b = 2.3;
var c = a + b;
var name1 = "Xiao Ming";
var name2 = "Xiaohong";

document.write(name1 + "ask" + name2 + ": " + a + "+" + b + "How much is it?" + "<br>");
document.write(name2 + "The answer is:" + a + "+" + b + "Be equal to" + c + "ah!" + "<br>");

document.write(a + name1 + b + name2);

</script>

</body>
</html>  

Result:

Xiaoming asks Xiaohong: how much is 1 + 2.3?
Xiaohong replied: 1 + 2.3 is equal to 3.3!
1 Xiaoming 2.3 Xiaohong

Tolerable.

3. Boolean type

True and false are represented by true and false respectively.

4. array

<!DOCTYPE html>
<html>
<body>

<script>
// Method 1
var i;
var cars1 = new Array();
cars1[0] = "Audi";
cars1[1] = "BMW";
cars1[2] = "Volvo";

for (i=0;i<cars1.length;i++)
{
document.write(cars1[i] + "<br>");
}

document.write("<br>");

// Method 2
var cars2 = new Array("Audi", "BMW", "Volvo");
for (i=0;i<cars2.length;i++)
{
document.write(cars2[i] + "<br>");
}
</script>

</body>
</html>

Result:

Audi
BMW
Volvo

Audi
BMW
Volvo

5. JavaScript object

<!DOCTYPE html>

<html>
<body>

<script charset = "utf-8">
var info = {
name: "Xiao Ming",
age: 63,
addr: "No. 156, ivory mountain village",
}

// There are two ways to address js objects
// Addressing 1
document.write(info.name + "<br>");
// Addressing 2
document.write(info["addr"] + "<br>");

</script>

</body>
</html>

 

At the beginning of running, the Chinese code is garbled. After adding charset = "utf-8" in < script charset = "utf-8" >.

Topics: Javascript