原文链接:Understanding Data Types in JavaScript

Data types are used to classify one particular type of data in programming languages. For instance, a number and a string of characters are different types of data that will be treated differently by JavaScript.

This is important because the specific data type you use will determine what values you can assign to it and what you can do to it. This is to say, to be able to do operations with variables in JavaScript, it is important to understand the data type of any given variable.

Dynamic Typing

JavaScript has dynamic data types, meaning that type checking is done at runtime rather than compile time. Python’s data types are also dynamically typed.

With dynamically typed languages, a variable of the same name can be used to hold different data types.

For example, the variable t, defined as a variable by the let keyword (note that let keeps a given variable limited in scope), can be assigned to hold different data types, or can be initialized but left undefined:

let t = 16;         // t is a number
let t = "Teresa"; // t is a string
let t = true; // t is a Boolean
let t; // t is undefined

  Each of the variables t above can be set to any data type available in JavaScript; they do not need to be explicitly declared with a data type before they are used.

Numbers

JavaScript has only one number type, there is no separate designation for integers and floating-point numbers. Because of this, numbers can be written in JavaScript with or without decimals:

let num1 = 93;
let num2 = 93.00;

  In both cases above, the data type is a number and is the same regardless of whether or not the number has decimal points.

Scientific exponential notation can be used in JavaScript to abbreviate very large or small numbers, as in the following examples:

let num3 = 987e8;       // 98700000000
let num4 = 987e-8; // 0.00000987

  

Numbers in JavaScript are considered to be accurate up to 15 digits. That means that numbers will be rounded after the 16th digit is reached:

let num5 = 999999999999999;     // remains as 999999999999999
let num6 = 9999999999999999; // rounded up to 10000000000000000

  

Infinity or -Infinity will be returned if you calculate a number outside of the largest possible number available in JavaScript. These will also occur for values that are undefined, as when dividing by zero:

let num7 = 5 / 0;   // will return Infinity
let num8 = -5 / 0; // will return -Infinity

  In technical terms, Infinity will be displayed when a number exceeds the number 1.797693134862315E+308, which represents the upper limit in JavaScript.

  Similarly, -Infinity will be displayed when a number goes beyond the lower limit of -1.797693134862316E+308.

The number Infinity can also be used in loops:

while (num9 != Infinity) {
// Code here will execute through num9 = Infinity
}

  

For numbers that are not legal numbers, NaN will be displayed. If you attempt to perform a mathematical operation on a number and a non-numeric value, NaN will be returned. This is the case in the following example:

let x = 20 / "Shark";   // x will be NaN

  Since the number 20 cannot be divided by the string "Shark" because it cannot be evaluated as a number, the returned value for the x variable is NaN.

However, if a string can be evaluated as a numeric value, the mathematical expression can be performed in JavaScript:

let y = 20 / "5";   // y will be 4

  In the above example, since the string "5" can be evaluated as a numeric value in JavaScript, it is treated as such and will work with the mathematical operator for division, /.

When assigning the value NaN to a variable used in an operation, it will result in the value of NaN, even when the other operand is a legal number:

let a = NaN;
let b = 37;
let c = a + b; // c will be NaN

  

There is only one number data type in JavaScript. When working with numbers, any number you enter will be interpreted as the data type for numbers; you are not required to declare what kind of data type you are entering because JavaScript is dynamically typed.

Strings

A string is a sequence of one or more characters (letters, numbers, symbols). Strings are useful in that they represent textual data.

In JavaScript, strings exist within either single quotes ' or double quotes ", so to create a string, enclose a sequence of characters in quotes:

let singleQuotes = 'This is a string in single quotes.';

let doubleQuotes = "This is a string in double quotes.";

  You can choose to use either single quotes or double quotes, but whichever you decide on you should remain consistent within a program.

There are many operations that we can perform on strings within our programs in order to manipulate them to achieve the results we are seeking. Strings are important for communicating information to the user, and for the user to communicate information back to the program.

Booleans

The Boolean data type can be one of two values, either true or false. Booleans are used to represent the truth values that are associated with the logic branch of mathematics, which informs algorithms in computer science.

Whenever you see the data type Boolean, it will start with a capitalized B because it is named for the mathematician George Boole.

Many operations in math give us answers that evaluate to either true or false:

  • greater than

    • 500 > 100 true
    • 1 > 5 false
  • less than
    • 200 < 400 true
    • 4 < 2 false
  • equal
    • 5 = 5 true
    • 500 = 400 false

Arrays

An array can hold multiple values within a single variable. This means that you can contain a list of values within an array and iterate through them.

Each item or value that is inside of an array is called an element. You can refer to the elements of an array by using an index number.

Just as strings are defined as characters between quotes, arrays are defined by having values between square brackets [ ]. 】

An array of strings, for example, looks like this:

let fish = ["shark", "cuttlefish", "clownfish", "eel"];

 

Arrays are a very flexible data type because they are mutable in that they can have element values added, removed, and changed. 

Objects

The JavaScript object data type can contain many values as name:value pairs. These pairs provide a useful way to store and access data. The object literal syntax is made up of name:value pairs separated by colons with curly braces on either side { }.

Typically used to hold data that are related, such as the information contained in an ID, a JavaScript object literal looks like this, with whitespaces between properties:

let sammy = {firstName:"Sammy", lastName:"Shark", color:"blue", location:"ocean"};

  

Alternatively, and especially for object literals with a high number of name:value pairs, we can write this data type on multiple lines, with a whitespace after each colon:

let sammy = {
firstName: "Sammy",
lastName: "Shark",
color: "blue",
location: "Ocean"
};

  The object variable sammy in each of the examples above has 4 properties: firstName, lastName, color, and location. These are each passed values separated by colons.

Working with Multiple Data Types

While each program you create will contain multiple data types, it is important to keep in mind that you will generally be performing operations within the same data type.

That is, you’ll be performing mathematics on numbers, or slicing strings.

When you use an operator that works across data types, like the + operator that can add numbers or concatenate strings, you may achieve unexpected results.

For example, when using the + operator with numbers and strings together, the numbers will be treated as a string (thus they will be concatenated), but the order of the data types will influence the concatenation.

So, if you create a variable that performs the following concatenation, JavaScript will interpret each element below as a string:

let o = "Ocean" + 5 + 3;

# Output
Ocean53

However, if you lead with numbers, the two numbers will be added before they are then interpreted as a string when the program runtime reaches "Ocean", so the returned value will be the sum of the two numbers concatenated with the string:

let p = 5 + 3 + "Ocean";

# Output
8Ocean

  

Because of these unexpected outcomes, you’ll likely be performing operations and methods within one data type rather than across them.

JavaScript, however, does not return errors when mixing data types, as some other programming languages do.

Understanding Objects in JavaScript

  

Javescript——数据类型的更多相关文章

  1. JaveScript基础(1)之变量和数据类型

    1.JaveScript变量的定义方式: A:隐式定义:直接给变量赋值: temp='hello'; alert(temp); PS:使用变量前要先进行初始化工作,否则会报变量未被定义的错误; B:显 ...

  2. JaveScript简单数据类型(JS知识点归纳二)

    JS中的简单数据类型有五种 : --> string --> number -->boolean --> null -->undefined 数据类型的检测 :typeo ...

  3. 前端开发JavaScript入门——JavaScript介绍&基本数据类型

    JavaScript 诞生于1995年,它的出现主要是用于处理网页中的 前端验证. • 所谓的前端验证,就是指检查用户输入的内容是否符合一定的 规则. • 比如:用户名的长度,密码的长度,邮箱的格式等 ...

  4. JavaScript 中的数据类型

    Javascript中的数据类型有以下几种情况: 基本类型:string,number,boolean 特殊类型:undefined,null 引用类型:Object,Function,Date,Ar ...

  5. JS 判断数据类型的三种方法

    说到数据类型,我们先理一下JavaScript中常见的几种数据类型: 基本类型:string,number,boolean 特殊类型:undefined,null 引用类型:Object,Functi ...

  6. Python高手之路【二】python基本数据类型

    一:数字 int int(整型): 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2147483648-2147483647 在64位系统上,整数的位数为64位,取值 ...

  7. UniqueIdentifier 数据类型 和 GUID 生成函数

    UniqueIdentifier 数据类型用于存储GUID的值,占用16Byte. SQL Server将UniqueIdentifier存储为16字节的二进制数值,Binary(16),按照特定的格 ...

  8. SQL Server常见数据类型介绍

    数据表是由多个列组成,创建表时必须明确每个列的数据类型,以下列举SQL Server常见数据类型的使用规则,方便查阅. 1.整数类型 int 存储范围是-2,147,483,648到2,147,483 ...

  9. 由js apply与call方法想到的js数据类型(原始类型和引用类型)

    原文地址:由js apply与call方法想到的js数据类型(原始类型和引用类型) js的call方法与apply方法的区别在于第二个参数的不同,他们都有2个参数,第一个为对象(即需要用对象a继承b, ...

随机推荐

  1. 《构建之法》第五次作业——Alpha项目测试

    博客开头 这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/2019autumnsystemanalysisanddesign?page=6 这个作业要求在 ...

  2. Java对象内存分配原理与布局

    当一个对象被创建了,那在JVM中是如何的从一个对象不存在到存到,然后将对象存放在什么地方呢?这次主要来探讨一下Java对象创建的过程. new关键字创建对象的3个步骤: 1.在堆内存中创建出对象的实例 ...

  3. 【数组模拟的链表or复杂模拟】PAT-L2-002. 链表去重

    L2-002. 链表去重 给定一个带整数键值的单链表L,本题要求你编写程序,删除那些键值的绝对值有重复的结点.即对任意键值K,只有键值或其绝对值等于K的第一个结点可以被保留.同时,所有被删除的结点必须 ...

  4. P3205 [HNOI2010]合唱队[区间dp]

    题目描述 为了在即将到来的晚会上有更好的演出效果,作为AAA合唱队负责人的小A需要将合唱队的人根据他们的身高排出一个队形.假定合唱队一共N个人,第i个人的身高为Hi米(1000<=Hi<= ...

  5. P2661 信息传递[最小环+边带权并查集]

    题目来源:洛谷 题目描述 有 n 个同学(编号为 1 到 n )正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为 i 的同学的信息传递对象是编号为 Ti​ 的同学. 游戏 ...

  6. 回调函数(callback)

    回调函数(callback) A "callback" is any function that is called by another function which takes ...

  7. 11 loader - 配置处理scss文件的loader

    1.装包 cnpm i sass-loader -D peerDependencies WARNING sass-loader@* requires a peer of node-sass@^4.0. ...

  8. Git----常见工作管理总结

    1.工作流程模式: 首先,可以试图用git push origin branch-name推送自己的修改 如果推送失败,则因为远程分支比你的本地更新,需要先用git pull试图合并 如果合并有冲突, ...

  9. Java:JVM垃圾回收(GC)机制

    JVM垃圾回收算法 1.标记清除(Mark-Sweep) 原理: 从根集合节点进行扫描,标记出所有的存活对象,最后扫描整个内存空间并清除没有标记的对象(即死亡对象)适用场合: 存活对象较多的情况下比较 ...

  10. Mina整体体系结构分析

    mina在应用程序中处于什么样的地位? mina屏蔽了一些网络通信细节对socket进行封装,并且基于NIO非阻塞框架,可以帮助我们快速开发网络通信,常常用于用户游戏开发,中间件等服务端应用程序.