JavaScript & TypeScript: How to Use Functions Inside Five
Tags: javascript, typescript, tutorial, applicationdevelopment
Published: 2023-10-08 22:13:09

<!-- wp:paragraph -->
<p>One of the most significant ways to control and reuse code is with the time-honored approach of bundling it into a named block of code that can be invoked repeatedly: we speak, of course, of the traditional function.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>JavaScript functions are essential building blocks in application development, enabling developers to create reusable blocks of code that perform specific tasks. They play a pivotal role in structuring and organizing code, promoting modularity and maintainability of applications.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In this article, we'll delve into the world of JavaScript functions, exploring their advantages and we will explain how to write functions inside of Five.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->

<!-- wp:rank-math/toc-block {"headings":[{"key":"9c74e2a1-a8cd-4954-a76f-6d629b60e796","content":"JavaScript: The Old Faithful","level":2,"link":"#java-script-the-old-faithful","disable":false,"isUpdated":false,"isGeneratedLink":true},{"key":"0ccedb03-0603-4070-b455-1554533f2f31","content":"Syntax and Flexibility","level":3,"link":"#syntax-and-flexibility","disable":true,"isUpdated":false,"isGeneratedLink":true},{"key":"07d35493-9035-4cae-a09c-99b1ab2b3205","content":"The JavaScript Ecosystem","level":3,"link":"#the-java-script-ecosystem","disable":true,"isUpdated":false,"isGeneratedLink":true},{"key":"81b16350-648b-4869-9d02-064e869fedf6","content":"TypeScript: The Type-Safe Alternative","level":2,"link":"#type-script-the-type-safe-alternative","disable":false,"isUpdated":false,"isGeneratedLink":true},{"key":"46a5fb33-28a3-4440-9ad3-0a2868dafd13","content":"Static Typing Advantages","level":3,"link":"#static-typing-advantages","disable":true,"isUpdated":false,"isGeneratedLink":true},{"key":"7e13e23b-accb-41db-8a36-c810d8d4c226","content":"Tooling and Development Experience","level":3,"link":"#tooling-and-development-experience","disable":true,"isUpdated":false,"isGeneratedLink":true},{"key":"2929dbca-058d-4918-8006-339d6151db53","content":"The TypeScript Ecosystem","level":3,"link":"#the-type-script-ecosystem","disable":true,"isUpdated":false,"isGeneratedLink":true},{"key":"c3d79cac-ab99-4a55-8b9e-ef22282d0c80","content":"What are JavaScript Functions?","level":2,"link":"#what-are-java-script-functions","disable":false,"isUpdated":false,"isGeneratedLink":true},{"key":"74202830-72f3-43ba-bff9-2c3d9cfbfcf2","content":"Use of JavaScript Functions","level":3,"link":"#use-of-java-script-functions","disable":false,"isUpdated":false,"isGeneratedLink":true},{"key":"3e412870-17b4-4b76-ba1d-042f72566141","content":"1. Using Functions with Events:","level":4,"link":"#1-using-functions-with-events","disable":false,"isUpdated":false,"isGeneratedLink":true},{"key":"811d4e6a-939c-4ff2-aa3f-4a2238c14c78","content":"2. Data Processing and Manipulation:","level":4,"link":"#2-data-processing-and-manipulation","disable":false,"isUpdated":false,"isGeneratedLink":true},{"key":"1f395fbb-043d-47d0-ab00-c68d67791085","content":"3. API Calls and Asynchronous Operations:","level":4,"link":"#3-api-calls-and-asynchronous-operations","disable":false,"isUpdated":false,"isGeneratedLink":true},{"key":"3b9d9d02-5697-4493-9976-301cb6ff07d5","content":"4. Code Reusability:","level":4,"link":"#4-code-reusability","disable":false,"isUpdated":false,"isGeneratedLink":true},{"key":"73c18b88-815a-461d-963b-116510ada4ca","content":"5. Modularity and Code Organization:","level":4,"link":"#5-modularity-and-code-organization","disable":false,"isUpdated":false,"isGeneratedLink":true},{"key":"79ba929a-2ad4-4b96-922b-0ef512894c87","content":"Using Functions in Five: JavaScript \u0026 TypeScript Code Editor","level":2,"link":"#using-functions-in-five-java-script-type-script-code-editor","disable":false,"isUpdated":false,"isGeneratedLink":true},{"key":"7e0f5059-cb6d-406a-a26a-986d14474752","content":"What is Five?","level":3,"link":"#what-is-five","disable":true,"isUpdated":false,"isGeneratedLink":true},{"key":"370a7b98-6ca4-4aa4-8090-4b6a7c934270","content":"Conclusion","level":2,"link":"#conclusion","disable":false,"isUpdated":false,"isGeneratedLink":true}],"listStyle":"ul","titleWrapper":"h3"} -->
<div class="wp-block-rank-math-toc-block" id="rank-math-toc"><nav><ul><li class=""><a href="#java-script-the-old-faithful">JavaScript: The Old Faithful</a><ul></ul></li><li class=""><a href="#type-script-the-type-safe-alternative">TypeScript: The Type-Safe Alternative</a><ul></ul></li><li class=""><a href="#what-are-java-script-functions">What are JavaScript Functions?</a><ul><li class=""><a href="#use-of-java-script-functions">Use of JavaScript Functions</a><ul><li class=""><a href="#1-using-functions-with-events">1. Using Functions with Events:</a></li><li class=""><a href="#2-data-processing-and-manipulation">2. Data Processing and Manipulation:</a></li><li class=""><a href="#3-api-calls-and-asynchronous-operations">3. API Calls and Asynchronous Operations:</a></li><li class=""><a href="#4-code-reusability">4. Code Reusability:</a></li><li class=""><a href="#5-modularity-and-code-organization">5. Modularity and Code Organization:</a></li></ul></li></ul></li><li class=""><a href="#using-functions-in-five-java-script-type-script-code-editor">Using Functions in Five: JavaScript &amp; TypeScript Code Editor</a><ul></ul></li><li class=""><a href="#conclusion">Conclusion</a></li></ul></nav></div>
<!-- /wp:rank-math/toc-block -->

<!-- wp:heading -->
<h2 class="wp-block-heading" id="java-script-the-old-faithful">JavaScript: The Old Faithful</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><span style="font-weight: 400;">JavaScript, often referred to as the "language of the web," is a dynamic, interpreted language that runs directly in the browser. It provides the foundation for building interactive and dynamic web pages. JavaScript's syntax is concise and flexible, allowing developers to write code quickly and efficiently. This simplicity makes it an ideal choice for beginners or those who want to prototype and develop small-scale projects rapidly.</span></p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1614} -->
<figure class="wp-block-image"><img src="https://five.co/wp-content/uploads/2023/08/blog-jsfunction-1024x451.png" alt="JavaScript functions" class="wp-image-1614"/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading" id="syntax-and-flexibility"><b>Syntax and Flexibility</b></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><span style="font-weight: 400;">One of JavaScript's key attractions is its familiar syntax. Its syntax resembles many other programming languages such as C, Java, and Python, making it easy to learn and adapt for developers coming from different backgrounds. For experienced programmers, the transition to JavaScript is usually seamless.</span></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><span style="font-weight: 400;">Another advantage is JavaScript's flexibility. It's a dynamically typed language, which means you don't need to explicitly declare variable types. This flexibility makes JavaScript forgiving, as it allows you to perform operations that other statically typed languages might consider errors. While this flexibility can lead to unexpected behavior if not handled carefully, it also enables rapid prototyping and quick iterations.</span></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading" id="the-java-script-ecosystem"><b>The JavaScript Ecosystem</b></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><span style="font-weight: 400;">JavaScript is known for its vast ecosystem of libraries, frameworks, and tools. The community-driven nature of JavaScript has led to the development of numerous open-source projects, making it easier for developers to find solutions to common problems. Popular JavaScript frameworks like React, Angular, and Vue.js have revolutionized web development by providing efficient and scalable ways to build user interfaces. Additionally, JavaScript has an expansive selection of libraries, such as Express.js and Redux, to address server-side, state management, and other specific needs.</span></p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading" id="type-script-the-type-safe-alternative">TypeScript: The Type-Safe Alternative</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><span style="font-weight: 400;">TypeScript is a superset of JavaScript, meaning it builds upon JavaScript by adding static typing. It aims to enhance JavaScript's capabilities by enabling developers to catch type-related bugs during the development phase, rather than at runtime. TypeScript code runs through a compilation process that checks for type errors and transpiles it into JavaScript, which can then be executed by browsers or other JavaScript environments.</span></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading" id="static-typing-advantages"><b>Static Typing Advantages</b></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><span style="font-weight: 400;">The key feature that sets TypeScript apart from JavaScript is static typing. With TypeScript, you can specify types for variables, function parameters, and return values. This additional layer of type checking catches potential errors and improves code quality. By catching type-related bugs earlier, TypeScript helps reduce the number of unforeseen issues that may arise during runtime.</span></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><span style="font-weight: 400;">Static typing also brings the advantage of better code documentation and improved readability. With explicitly defined types, developers can easily understand the expected inputs and outputs of functions, making code easier to understand and maintain. IDEs and code editors that support TypeScript can provide enhanced features such as autocompletion, refactoring tools, and immediate feedback on potential type mismatches.</span></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading" id="tooling-and-development-experience"><b>Tooling and Development Experience</b></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><span style="font-weight: 400;">Alongside static typing, TypeScript provides a robust set of tools to enhance the development experience. The TypeScript compiler, </span><span style="font-weight: 400;">tsc</span><span style="font-weight: 400;">, offers a wide range of configuration options and can integrate with popular build systems such as Webpack or Babel. Additionally, TypeScript provides valuable features like code analysis, linting, and advanced language services to identify potential issues and improve code quality.</span></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><span style="font-weight: 400;">Another advantage of TypeScript is its compatibility with JavaScript. Since TypeScript is a superset of JavaScript, existing JavaScript code can be gradually migrated to TypeScript without any major alterations. Developers familiar with JavaScript can write TypeScript code from the start or begin with JavaScript and gradually introduce TypeScript's features as the project complexity increases.</span></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading" id="the-type-script-ecosystem"><b>The TypeScript Ecosystem</b></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><span style="font-weight: 400;">TypeScript adoption has been gaining momentum, and as a result, it now enjoys widespread community support and an expanding ecosystem. Many popular JavaScript libraries and frameworks, including React, Angular, and Vue.js, have dedicated TypeScript support, providing type definitions that enable developers to write TypeScript code with ease.</span></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><span style="font-weight: 400;">TypeScript also benefits from its close relationship with Microsoft. With robust documentation, official guides, and active development, TypeScript provides a level of stability and assurance to developers. This close relationship results in continuous improvements to the language and its tooling, ensuring that TypeScript remains a reliable and efficient choice for modern web development.</span></p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading" id="what-are-java-script-functions">What are JavaScript Functions?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>A function is a block of code that is used to perform a single, related action. Functions provide better modularity for your application as well as high code reusability.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading" id="use-of-java-script-functions">Use of JavaScript Functions</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>JavaScript functions have a wide range of use cases due to their versatility and ability to encapsulate code logic. Here are some typical use cases for JavaScript functions:</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1615} -->
<figure class="wp-block-image"><img src="https://five.co/wp-content/uploads/2023/08/blog-jsexamples-1024x451.png" alt="Uses of JavaScript functions" class="wp-image-1615"/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":4} -->
<h4 class="wp-block-heading" id="1-using-functions-with-events"><strong>1. Using Functions with Events:</strong></h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Functions are commonly used as event handlers to respond to user interactions like button clicks, form submissions, or mouse movements. By attaching functions to events, developers can define specific actions or behaviors in response to user actions.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4 class="wp-block-heading" id="2-data-processing-and-manipulation"><strong>2. Data Processing and Manipulation:</strong></h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Functions are frequently used for data processing and manipulation tasks, such as filtering, sorting, or transforming data. They enable developers to create reusable data processing pipelines, making it easier to work with complex datasets.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4 class="wp-block-heading" id="3-api-calls-and-asynchronous-operations"><strong>3. API Calls and Asynchronous Operations:</strong></h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>JavaScript functions are essential when working with APIs or performing asynchronous operations like fetching data from a server. Functions can be used to encapsulate API calls and handle responses, making the code more organized and reusable.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4 class="wp-block-heading" id="4-code-reusability"><strong>4. Code Reusability:</strong></h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Functions are ideal for promoting code reusability. Instead of repeating the same code in multiple places, developers can define functions once and reuse them throughout the codebase. This simplifies maintenance, improves readability, and reduces the chances of introducing bugs.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4 class="wp-block-heading" id="5-modularity-and-code-organization"><strong>5. Modularity and Code Organization:</strong></h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>JavaScript functions facilitate code modularity and organization. By breaking down complex tasks into smaller, self-contained functions, developers can enhance code readability, maintainability, and collaboration among team members.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading" id="using-functions-in-five-java-script-type-script-code-editor">Using Functions in Five: JavaScript &amp; TypeScript Code Editor</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Inside Five, developers can create JavaScript or TypeScript functions, using Five's code editor. The code editor comes with helpful features, such as syntax highlighting, autocompletion, or a co-pilot for interpreting and debugging code.</p>
<!-- /wp:paragraph -->

<!-- wp:separator -->
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<!-- /wp:separator -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading" id="what-is-five"><strong>What is Five?</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Five is a low-code solution that helps developers rapidly build &amp; deploy online database applications. Five provides pre-built features, such as a fully provisioned MySQL database, authentication, and one-click application deployment—or table, form, and chart wizards. Developers can also create logic by writing JavaScript or TypeScript functions in Five.</p>
<!-- /wp:paragraph -->

<!-- wp:separator -->
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<!-- /wp:separator -->

<!-- wp:paragraph -->
<p>To access the code editor, open Five and select one of your applications. Next, click on <strong>Manage</strong>, then on <strong>Logic, </strong>and then <strong>Code Editor.&nbsp;</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Now click on the <strong>Plus</strong> icon. Choose a name for your function, as well as the language you'd like to use: JavaScript or TypeScript.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1618,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://five.co/wp-content/uploads/2023/08/Frame-18-2-1024x548.png" alt="" class="wp-image-1618"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>You can now write standard JavaScript or TypeScript to create functions right inside of Five. In this tutorial, we will simply test our function by attaching it to a process and testing if our function will do the desired task whenever the process is completed.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let's begin by creating a process to which we will attach our function. Start by navigating to processes: click on 'Tasks' and then 'Processes'</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1619,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://five.co/wp-content/uploads/2023/08/Frame-20-1024x190.png" alt="" class="wp-image-1619"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Next, create a new process by clicking on the yellow '+' button and giving it a name. Now click on the '<strong>Fields</strong>' tab and create a new field called <strong>Name</strong> and give it a display type of <em>_Text</em>. </p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1621,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://five.co/wp-content/uploads/2023/08/Frame-21-1024x548.png" alt="" class="wp-image-1621"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>After completing our process, let's return to the code editor and create a function.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To begin, we can write a simple log statement to test our function. In Five, you can use <code>console.log()</code> , the same way you would using standard JavaScript / TypeScript. However, for server-side events, we utilize <code>five.log()</code>. Take a look at the snippet below ( we will be using JavaScript to keep things simple )</p>
<!-- /wp:paragraph -->

<!-- wp:kevinbatdorf/code-block-pro {"code":"function logGreeting(five, context, result) {\n    five.log(\u0022Hello\u0022);\n    return five.success(result);\n}","codeHTML":"\u003cpre class=\u0022shiki one-dark-pro\u0022 style=\u0022background-color: #282c34\u0022 tabindex=\u00220\u0022\u003e\u003ccode\u003e\u003cspan class=\u0022line\u0022\u003e\u003cspan style=\u0022color: #C678DD\u0022\u003efunction\u003c/span\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e \u003c/span\u003e\u003cspan style=\u0022color: #61AFEF\u0022\u003elogGreeting\u003c/span\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e(\u003c/span\u003e\u003cspan style=\u0022color: #E06C75; font-style: italic\u0022\u003efive\u003c/span\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e, \u003c/span\u003e\u003cspan style=\u0022color: #E06C75; font-style: italic\u0022\u003econtext\u003c/span\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e, \u003c/span\u003e\u003cspan style=\u0022color: #E06C75; font-style: italic\u0022\u003eresult\u003c/span\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e) {\u003c/span\u003e\u003c/span\u003e\n\u003cspan class=\u0022line\u0022\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e    \u003c/span\u003e\u003cspan style=\u0022color: #E5C07B\u0022\u003efive\u003c/span\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e.\u003c/span\u003e\u003cspan style=\u0022color: #61AFEF\u0022\u003elog\u003c/span\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e(\u003c/span\u003e\u003cspan style=\u0022color: #98C379\u0022\u003e\u0026quot;Hello\u0026quot;\u003c/span\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e);\u003c/span\u003e\u003c/span\u003e\n\u003cspan class=\u0022line\u0022\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e    \u003c/span\u003e\u003cspan style=\u0022color: #C678DD\u0022\u003ereturn\u003c/span\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e \u003c/span\u003e\u003cspan style=\u0022color: #E5C07B\u0022\u003efive\u003c/span\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e.\u003c/span\u003e\u003cspan style=\u0022color: #61AFEF\u0022\u003esuccess\u003c/span\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e(\u003c/span\u003e\u003cspan style=\u0022color: #E06C75\u0022\u003eresult\u003c/span\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e);\u003c/span\u003e\u003c/span\u003e\n\u003cspan class=\u0022line\u0022\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e}\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e","language":"javascript","theme":"one-dark-pro","bgColor":"#282c34","textColor":"#abb2bf","fontSize":".875rem","fontFamily":"Code-Pro-JetBrains-Mono","lineHeight":"1.25rem","headerType":"headlights","lineHighlightColor":"rgba(134, 167, 228, 0.2)","copyButton":true,"buttonTheme":"heroicons"} -->
<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282c34"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="function logGreeting(five, context, result) {
    five.log(&quot;Hello&quot;);
    return five.success(result);
}" style="color:#abb2bf;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki one-dark-pro" style="background-color: #282c34" tabindex="0"><code><span class="line"><span style="color: #C678DD">function</span><span style="color: #ABB2BF"> </span><span style="color: #61AFEF">logGreeting</span><span style="color: #ABB2BF">(</span><span style="color: #E06C75; font-style: italic">five</span><span style="color: #ABB2BF">, </span><span style="color: #E06C75; font-style: italic">context</span><span style="color: #ABB2BF">, </span><span style="color: #E06C75; font-style: italic">result</span><span style="color: #ABB2BF">) {</span></span>
<span class="line"><span style="color: #ABB2BF">    </span><span style="color: #E5C07B">five</span><span style="color: #ABB2BF">.</span><span style="color: #61AFEF">log</span><span style="color: #ABB2BF">(</span><span style="color: #98C379">&quot;Hello&quot;</span><span style="color: #ABB2BF">);</span></span>
<span class="line"><span style="color: #ABB2BF">    </span><span style="color: #C678DD">return</span><span style="color: #ABB2BF"> </span><span style="color: #E5C07B">five</span><span style="color: #ABB2BF">.</span><span style="color: #61AFEF">success</span><span style="color: #ABB2BF">(</span><span style="color: #E06C75">result</span><span style="color: #ABB2BF">);</span></span>
<span class="line"><span style="color: #ABB2BF">}</span></span></code></pre></div>
<!-- /wp:kevinbatdorf/code-block-pro -->

<!-- wp:paragraph -->
<p>You might have noticed that functions within Five have three default arguments: <code>five</code>, <code>context</code>, and <code>result</code>. We've already utilized <code>five</code> for logging, and we passed the <code>result</code> to <code>five.success()</code> to indicate that the function was executed without errors.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Now, let's delve into the <code>context</code>. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In Five, the <code>context</code> variable encompasses all the data associated with the action to which the function is linked. For instance, if we connect our function to the process we made earlier, the <code>context</code> variable will capture whatever input we provide in the Name field.</p>
<!-- /wp:paragraph -->

<!-- wp:kevinbatdorf/code-block-pro {"code":"function logGreeting(five, context, result) {\n    const name = context.Name\n    five.log(\u0022Hello \u0022  + name);\n    return five.success(result);\n}","codeHTML":"\u003cpre class=\u0022shiki one-dark-pro\u0022 style=\u0022background-color: #282c34\u0022 tabindex=\u00220\u0022\u003e\u003ccode\u003e\u003cspan class=\u0022line\u0022\u003e\u003cspan style=\u0022color: #C678DD\u0022\u003efunction\u003c/span\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e \u003c/span\u003e\u003cspan style=\u0022color: #61AFEF\u0022\u003elogGreeting\u003c/span\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e(\u003c/span\u003e\u003cspan style=\u0022color: #E06C75; font-style: italic\u0022\u003efive\u003c/span\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e, \u003c/span\u003e\u003cspan style=\u0022color: #E06C75; font-style: italic\u0022\u003econtext\u003c/span\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e, \u003c/span\u003e\u003cspan style=\u0022color: #E06C75; font-style: italic\u0022\u003eresult\u003c/span\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e) {\u003c/span\u003e\u003c/span\u003e\n\u003cspan class=\u0022line\u0022\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e    \u003c/span\u003e\u003cspan style=\u0022color: #C678DD\u0022\u003econst\u003c/span\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e \u003c/span\u003e\u003cspan style=\u0022color: #E5C07B\u0022\u003ename\u003c/span\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e \u003c/span\u003e\u003cspan style=\u0022color: #56B6C2\u0022\u003e=\u003c/span\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e \u003c/span\u003e\u003cspan style=\u0022color: #E5C07B\u0022\u003econtext\u003c/span\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e.\u003c/span\u003e\u003cspan style=\u0022color: #E06C75\u0022\u003eName\u003c/span\u003e\u003c/span\u003e\n\u003cspan class=\u0022line\u0022\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e    \u003c/span\u003e\u003cspan style=\u0022color: #E5C07B\u0022\u003efive\u003c/span\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e.\u003c/span\u003e\u003cspan style=\u0022color: #61AFEF\u0022\u003elog\u003c/span\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e(\u003c/span\u003e\u003cspan style=\u0022color: #98C379\u0022\u003e\u0026quot;Hello \u0026quot;\u003c/span\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e  \u003c/span\u003e\u003cspan style=\u0022color: #56B6C2\u0022\u003e+\u003c/span\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e \u003c/span\u003e\u003cspan style=\u0022color: #E06C75\u0022\u003ename\u003c/span\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e);\u003c/span\u003e\u003c/span\u003e\n\u003cspan class=\u0022line\u0022\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e    \u003c/span\u003e\u003cspan style=\u0022color: #C678DD\u0022\u003ereturn\u003c/span\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e \u003c/span\u003e\u003cspan style=\u0022color: #E5C07B\u0022\u003efive\u003c/span\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e.\u003c/span\u003e\u003cspan style=\u0022color: #61AFEF\u0022\u003esuccess\u003c/span\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e(\u003c/span\u003e\u003cspan style=\u0022color: #E06C75\u0022\u003eresult\u003c/span\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e);\u003c/span\u003e\u003c/span\u003e\n\u003cspan class=\u0022line\u0022\u003e\u003cspan style=\u0022color: #ABB2BF\u0022\u003e}\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e","language":"javascript","theme":"one-dark-pro","bgColor":"#282c34","textColor":"#abb2bf","fontSize":".875rem","fontFamily":"Code-Pro-JetBrains-Mono","lineHeight":"1.25rem","headerType":"headlights","lineHighlightColor":"rgba(134, 167, 228, 0.2)","copyButton":true,"buttonTheme":"heroicons"} -->
<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282c34"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="function logGreeting(five, context, result) {
    const name = context.Name
    five.log(&quot;Hello &quot;  + name);
    return five.success(result);
}" style="color:#abb2bf;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki one-dark-pro" style="background-color: #282c34" tabindex="0"><code><span class="line"><span style="color: #C678DD">function</span><span style="color: #ABB2BF"> </span><span style="color: #61AFEF">logGreeting</span><span style="color: #ABB2BF">(</span><span style="color: #E06C75; font-style: italic">five</span><span style="color: #ABB2BF">, </span><span style="color: #E06C75; font-style: italic">context</span><span style="color: #ABB2BF">, </span><span style="color: #E06C75; font-style: italic">result</span><span style="color: #ABB2BF">) {</span></span>
<span class="line"><span style="color: #ABB2BF">    </span><span style="color: #C678DD">const</span><span style="color: #ABB2BF"> </span><span style="color: #E5C07B">name</span><span style="color: #ABB2BF"> </span><span style="color: #56B6C2">=</span><span style="color: #ABB2BF"> </span><span style="color: #E5C07B">context</span><span style="color: #ABB2BF">.</span><span style="color: #E06C75">Name</span></span>
<span class="line"><span style="color: #ABB2BF">    </span><span style="color: #E5C07B">five</span><span style="color: #ABB2BF">.</span><span style="color: #61AFEF">log</span><span style="color: #ABB2BF">(</span><span style="color: #98C379">&quot;Hello &quot;</span><span style="color: #ABB2BF">  </span><span style="color: #56B6C2">+</span><span style="color: #ABB2BF"> </span><span style="color: #E06C75">name</span><span style="color: #ABB2BF">);</span></span>
<span class="line"><span style="color: #ABB2BF">    </span><span style="color: #C678DD">return</span><span style="color: #ABB2BF"> </span><span style="color: #E5C07B">five</span><span style="color: #ABB2BF">.</span><span style="color: #61AFEF">success</span><span style="color: #ABB2BF">(</span><span style="color: #E06C75">result</span><span style="color: #ABB2BF">);</span></span>
<span class="line"><span style="color: #ABB2BF">}</span></span></code></pre></div>
<!-- /wp:kevinbatdorf/code-block-pro -->

<!-- wp:paragraph -->
<p>We've modified our function to extract the value from the 'Name' field within 'context'. We then log this value using <code>five.log()</code>. Finally, we return the outcome using <code>five.success()</code></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Next, let's attach our function to our process. Start by navigating back to 'Processes' inside the 'Task'  menu, select the process that we created earlier, and then click on the events tab which will show all the events that a process goes through. Here, <a href="https://help.five.org/2.1/docs/api/Events/introduction-to-fives-events" data-type="URL" data-id="https://help.five.org/2.1/docs/api/Events/introduction-to-fives-events">attach the function in the 'Do Complete'</a> event</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1622,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://five.co/wp-content/uploads/2023/08/Frame-22-1024x548.png" alt="" class="wp-image-1622"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Now let's finally put our process into a menu so that we can access it in our end-user application.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Navigate to '<strong>Visual</strong>' and then click on '<strong>Menu</strong>', we will create a new menu item, and then in the '<strong>Action</strong>' field we will select our process </p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1623,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://five.co/wp-content/uploads/2023/08/Frame-23-1024x548.png" alt="" class="wp-image-1623"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Finally let's run our application, to test our function we will run it in debug mode and that can be done by simply clicking on the ▶️ with the magnifying glass on the header.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1624,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://five.co/wp-content/uploads/2023/08/Frame-24-1024x190.png" alt="" class="wp-image-1624"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Five will now run your application. But now you'll also have access to the inspector, which you will be able to find minimized on the right. Maximize it by clicking on the 🔎 icon which will cause it to expand.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Now simply put your name in the Name field and then run the process by clicking on the ✔️icon on the top right. You'll see that you have successfully logged your message along with your name in the inspector.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1625,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://five.co/wp-content/uploads/2023/08/Frame-25-1024x548.png" alt="" class="wp-image-1625"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Congratulations you have successfully written and executed your function in Five.</p>
<!-- /wp:paragraph -->

<!-- wp:separator -->
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<!-- /wp:separator -->

<!-- wp:heading -->
<h2 class="wp-block-heading" id="conclusion">Conclusion</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>JavaScript and TypeScript functions are fundamental aspects of web development, offering a multitude of advantages. By leveraging functions effectively, developers can streamline their code, enhance maintainability, and enrich the overall user experience. Additionally, well-optimized JavaScript and TypeScript functions are essential for search engine optimization, leading to quicker page loads and better rankings.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If you are a beginner or looking for a language with less strict conventions and a broader community base, JavaScript is the way to go. With its established presence and rich ecosystem.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>However, if you aim for better maintainability, scalability, and the benefits of static typing, TypeScript should be your choice. TypeScript empowers developers to catch bugs early, improve code readability, and leverage code intelligence provided by modern IDEs and editors</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Through this tutorial, we've explored the foundational concepts of these languages, their unique features, and their application within the Five environment. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>By harnessing the capabilities of Five's code editor and the platform's event-driven architecture, developers can seamlessly integrate custom logic into their applications. Whether you're a seasoned developer or just starting out, the combination of JavaScript or TypeScript with Five's robust toolset offers a dynamic and efficient way to build applications. Dive in, experiment, and unlock the full potential of your web development journey with Five.</p>
<!-- /wp:paragraph -->