Simple Javascript Function Examples For Beginners With Code

JavaScript, a high-level, dynamic, and versatile programming language, comes with numerous built-in functions ready to put to use right away. Understanding these functions is key to becoming proficient in JavaScript. Today, we're going to explore some simple yet powerful JavaScript function examples, categorized for your ease.

How to Create a Function in JavaScript: Step-by-Step Guide 🚀
How to Create a Function in JavaScript: Step-by-Step Guide 🚀

Diving right in, let's first discuss some fundamental JavaScript functions that can greatly simplify your coding process.

Functions in Javascript
Functions in Javascript

Mathematical Functions

JavaScript offers several built-in functions to perform mathematical operations. Here are two of the most commonly used:

A quick intro to Higher-Order Functions in JavaScript
A quick intro to Higher-Order Functions in JavaScript

Math.random()

The Math.random() function generates a random decimal number. Its range is 0 (inclusive) to 1 (exclusive).

JavaScript Arrow Functions: Write Shorter & Cleaner Code 🚀
JavaScript Arrow Functions: Write Shorter & Cleaner Code 🚀

Here's an example of generating a random integer between 1 and 10:

let randomNum = Math.floor(Math.random() * 10) + 1;

Math.pow(base, exponent)

This function returns the base to the power of the exponent.

JS Functions 5 Min Guide for Beginners
JS Functions 5 Min Guide for Beginners

Let's square a number and cube it using this function:

let square = Math.pow(2, 2); // 4 let cube = Math.pow(2, 3); // 8

String Manipulation Functions

JavaScript provides several functions to manipulate strings, making working with text a breeze.

⏳ Master JavaScript setTimeout(): Delay Code Execution Like a Pro! 💻✨  Pin Description:
⏳ Master JavaScript setTimeout(): Delay Code Execution Like a Pro! 💻✨ Pin Description:

String.toLowerCase() and String.toUpperCase()

The toLowerCase() function converts a string to lowercase, while toUpperCase() converts it to uppercase.

Build - IN Function in JavaScript
Build - IN Function in JavaScript
JavaScript Function Basics – What Will This Return? #rupesh
JavaScript Function Basics – What Will This Return? #rupesh
an open notebook with some type of function written in red on the top and bottom
an open notebook with some type of function written in red on the top and bottom
Coding Tips For Beginners, Javascript Function Types, Understanding Javascript Syntax, Learn Javascript Functions, Javascript Functions List, Javascript Function Expression, Javascript Functions Tutorial, Javascript Functions
Coding Tips For Beginners, Javascript Function Types, Understanding Javascript Syntax, Learn Javascript Functions, Javascript Functions List, Javascript Function Expression, Javascript Functions Tutorial, Javascript Functions
Javascript Fundamental
Javascript Fundamental
JS Functions
JS Functions
Javascript Function
Javascript Function
JavaScript Basics for Beginners | Learn JS Step by Step
JavaScript Basics for Beginners | Learn JS Step by Step
Java Developer - Built in Function
Java Developer - Built in Function
JavaScript Higher-Order Functions — A Simple Explanation
JavaScript Higher-Order Functions — A Simple Explanation
✨ Master JavaScript Filter Function: Simplify Your Code Like a Pro! ✨
✨ Master JavaScript Filter Function: Simplify Your Code Like a Pro! ✨
10 JavaScript Concepts Every Beginner Must Know (Save This Cheat Sheet!)
10 JavaScript Concepts Every Beginner Must Know (Save This Cheat Sheet!)
3 Ways to Define Functions in JavaScript
3 Ways to Define Functions in JavaScript
Jsonpath in Javascript Functionality And Use Cases
Jsonpath in Javascript Functionality And Use Cases
Math Functions
Math Functions
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
the simple example to understand setInterval function
the simple example to understand setInterval function
JavaScript Fundamental
JavaScript Fundamental
JavaScript Manipulating Arrays 🚀 Follow for more 💡🌐🚀
JavaScript Manipulating Arrays 🚀 Follow for more 💡🌐🚀
5 ways to define a function in JavaScript
5 ways to define a function in JavaScript

Here's how you can use these functions to change the casing of a string:

let text = "Hello, World!"; let lowerCase = text.toLowerCase(); // "hello, world!" let upperCase = text.toUpperCase(); // "HELLO, WORLD!"

String.slice(start, end)

The slice() function extracts a portion of a string and returns the extracted section in a new string.

Let's slice a string from index 7 to 12:

let slicedText = text.slice(7, 12); // "World"

These simple JavaScript functions are just the tip of the iceberg. With continued practice and exploration, you'll discover even more powerful and versatile functions to streamline your coding process.

Now that you've seen these examples, why not try them out in your own projects? Practice makes perfect, after all. Happy coding!

Related Articles

Html Javascript Hover Event What Is Noscript In Html W3schools For Each Google Maps Javascript Examples W3 Js Json What Does Noscript Do In Html W3 Null Simple Javascript Examples For Beginners W3schools Javascript Get Url Parameters W3schools Modulus