You may need to use return in JS, summarize and explain! ! !

tags: JS basics  js  javascript

return value

The Return statement terminates the function execution and returns a specified value to the caller of the function (function name + () is equal to the return value of the function)
If there is no return value after return, undefined will be returned by default
The return value of the function can be any data type

 // function fn(){
        // console.log("Hello");
        // }
        // fn();
        function fn() {
            return "Hello";
        }
        fn(); // fn() === hello
        console.log(fn());

        var div = function (a, b) {
            return a / b;
        }
        var result = div(81, 9);
        console.log(result);

       //Return the specified array
        var arr = [];
        function getArr(a) {
            for (let i = 0; i < a; i++) {
                arr.push(i + 1);
            }
            return arr;
        }
        var result1 = getArr(8);
        console.log(result1);
         //Return a~b array
        var arr1 = [];
        function getArr1(a, b) {
            for (let i = a; i <= b; i++) {
                arr1.push(i);
            }
            return arr1;
        }
        console.log(getArr1(2, 6));

       // reverse the order
        var arr2 = [];
        function getArr2(a, b) {
            if (a < b) {
                for (let i = a; i <= b; i++) {
                    arr2.push(i);
                }
            } else {
                for (let i = b; i <= a; i++) {
                    arr2.push(i)
                }
            }
            return arr2;
        }
        console.log(getArr2(12, 6));

Intelligent Recommendation

May you return as a teenager

On the line! On the line! My personal blog is officially launched this time! My personal blog address: http://vogos.cn/ I should have not written an article for almost half a month. I have been studyi...

Use code to explain why you need an extension-oriented design

In basic object-oriented programming, you can only directly call the methods of a class, and these methods are defined by the author of this class, which is no problem for user-oriented classes. In ad...

Oracle often uses statements that you may need to use sometimes

Written in front: The following are some of the statements that may need to be used in the work. Some of the statements that have been sorted out by various sorts are useful. If the query is incorrect...

Use Scala Map's mapValues ​​with caution, you may need transform

Before stepping on the pit of mapValues, I believe most people would think that the logic of mapValues ​​is the same as all other map methods: apply a map function to all values ​​in the Map and retur...

Points you may need to pay attention to during the use of RestTemplate

When RestTemplate sets parameters in a get request, the parameters followed by the url must not be the param value after encode, because it will encode again A URL can be encoded multiple times, each ...

More Recommendation

【you may need to restart the kernel to use updated packages】

@PIP Insatll command is not installed in the third party library you may need to restart the kernel to use updated packages When the PIP Install command cannot be installed in the Jupyter, the third -...

Summarize some of the JS code snippets that you use in HTML pages.

[Navigation effect] After the page is rolled down 100 pixels,id="fixed"Add oneclass="shrink", removed when moved back.class="shrink" [Navigation effect] When the page is ...

Error: Module parse failed: 'return' outside of function (4:1) You may need an appropriate loader

Article directory 1. Bugs that appear Two, error analysis 3. My solution 1. Bugs that appear Two, error analysis In the docs there are import imported, but not used 3. My solution Check the file forim...

Annotation - you may need to know these

In the daily development work, especially when using some popular open source frameworks, we inevitably use annotations, the scope of annotations is more and more extensive, and after using annotation...

HashMap - you may need to know these

HashMap is a mapping data type that Android programmers (and of course Java programmers) often use. With the JDK version update, JDK1.8 has some optimizations compared to the underlying implementation...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top