Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the reference data types in javascript

Shulou Source: shulou.com Published: 2022-06-02 07:34:08 09月19日 Update

This article introduces the relevant knowledge of "what are the reference data types in javascript". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

There are three reference data types in javascript: 1, object (Object) type, which is an unordered set of keys and values; 2, Array type, which is a set of data arranged in order; and 3, function (Function) type, which is a block of code with specific functions.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Reference data types in JavaScript: object (Object), array (Array), function (Function).

1. Object type

The Object type in JavaScript is an unordered set of keys and values. Curly braces {} are required to define the object type. The syntax format is as follows:

{name1: value1, name2: value2, name3: value3,..., nameN: valueN}

Name1, name2, name3,..., and nameN are the keys in the object, and value1, value2, value3,..., and valueN are the corresponding values.

In JavaScript, the keys of the object type are of string type, and the value can be of any data type. To get a value in an object, you can use the object name. The form of the key, as shown in the following example:

Var person = {name: 'Bob', age: 20, tags: [' js', 'web',' mobile'], city: 'Beijing', hasCar: true, zipcode: null}; console.log (person.name); / / output Bobconsole.log (person.age); / / output 20

2. Array type

An Array is a collection of data arranged in order, each value in the array is called an element, and the array can contain any type of data. Defining an array in JavaScript requires square brackets [], and each element in the array is separated by a comma, for example:

[1,2,3, 'hello', true, null]

Alternatively, you can use the Array () function to create an array, as shown in the following example:

Var arr = new Array (1,2,3,4); console.log (arr); / / output [1,2,3,4]

The elements in the array can be accessed by index. The index in the array starts at 0 and increases in turn, that is, the index of the first element of the array is 0, the index of the second element is 1, the index of the third element is 2, and so on. As shown in the following example:

Var arr = [1,2,3.14, 'Hello', null, true]; console.log (arr [0]); / / output the element with index 0, that is, 1console.log (arr [5]); / / output the element with index 5, that is, trueconsole.log (arr [6]); / / the index is out of range and returns undefined

3. Function type

A function (Function) is a block of code with specific functions. The function does not run automatically, but needs to be called through the function name, as shown in the following example:

Function sayHello (name) {return "Hello," + name;} var res = sayHello ("Peter"); console.log (res); / / output Hello, Peter

In addition, functions can be stored in variables, objects, arrays, and functions can be passed as arguments to or returned from other functions, as shown in the following example:

Var fun = function () {console.log ("http://c.biancheng.net/js/");}function createGreeting (name) {return" Hello, "+ name;} function displayGreeting (greetingFunction, userName) {return greetingFunction (userName);} var result = displayGreeting (createGreeting," Peter "); console.log (result); / / output Hello, Peter" what are the reference data types in javascript "that's all, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Type array function object data element output index code content function more knowledge order run practical and so on. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker OPPO Reno Linux Xiaomi Huawei