Java Script Functions
Java Script Functions
============================InDetail==============================
1. parseInt():
• Definition: Parses a string and returns an integer.
• Syntax: parseInt(string, radix)
• Example:
let str = "123";
let num = parseInt(str);
console.log(num); // Output: 123
2. parseFloat():
• Definition: Parses a string and returns a floating point number.
• Syntax: parseFloat(string)
• Example:
let str = "3.14";
let num = parseFloat(str);
console.log(num); // Output: 3.14
3. console.log():
• Definition: Outputs messages to the console.
• Syntax: console.log(value1, value2, ..., valueN)
• Example:
console.log("Hello, world!");
4. function():
• Definition: Keyword to declare a function.
• Syntax: function functionName(parameters) { // function body }
• Example:
function greet(name)
{
console.log("Hello, " + name + "!");
}
5. functionName():
• Definition: Syntax to call a function.
• Syntax: functionName(arguments)
• Example:
greet("Alice");
6. return:
• Definition: Statement to explicitly return a value from a function.
• Syntax: return expression;
• Example:
function add(a, b) { return a + b; }
7. arguments:
• Definition: Array-like object representing function arguments.
• Syntax: arguments[index]
• Example:
function sum()
{
let total = 0;
for (let i = 0; i < arguments.length; i++)
{
total += arguments[i];
}
return total;
}
8. length:
• Definition: Property of the arguments object to determine the
number of arguments.
• Syntax: arguments.length
• Example:
function countArgs()
{
return arguments.length;
}
9. if statement:
• Definition: Conditional statement to execute code based on a
condition.
• Syntax: if (condition) { // code block }
• Example:
let num = 10;
if (num > 0) {
console.log("Number is positive");
}
10.for loop:
• Definition: Iterates over a block of code a number of times.
• Syntax: for (initialization; condition; iteration) { // code block }
• Example:
for (let i = 0; i < 5; i++)
{
console.log(i);
}
11.typeof:
• Definition: Operator to determine the type of a variable.
• Syntax: typeof variable
• Example:
let x = 10;
console.log(typeof x); // Output: "number"
12.isNaN():
• Definition: Function to determine if a value is NaN (Not a
Number).
• Syntax: isNaN(value)
• Example:
console.log(isNaN("Hello")); // Output: true
13.Array.isArray():
• Definition: Function to determine if a value is an array.
• Syntax: Array.isArray(value)
• Example:
let arr = [1, 2, 3];
console.log(Array.isArray(arr)); // Output: true
14.Math.random():
• Definition: Returns a random floating-point number between 0
and 1.
• Syntax: Math.random()
• Example:
console.log(Math.random()); // Output: 0.12345 (random
number)
15.Math.floor():
• Definition: Rounds a number down to the nearest integer.
• Syntax: Math.floor(number)
• Example:
console.log(Math.floor(3.7)); // Output: 3
16.Math.ceil():
• Definition: Rounds a number up to the nearest integer.
• Syntax: Math.ceil(number)
• Example:
console.log(Math.ceil(3.2)); // Output: 4
17.Math.max():
• Definition: Returns the highest-valued number in a list of
arguments.
• Syntax: Math.max(value1, value2, ..., valueN)
• Example:
console.log(Math.max(5, 10, 2)); // Output: 10
18.Math.min():
• Definition: Returns the lowest-valued number in a list of
arguments.
• Syntax: Math.min(value1, value2, ..., valueN)
• Example:
console.log(Math.min(5, 10, 2)); // Output: 2
19.String():
• Definition: Converts a value into a string.
• Syntax: String(value)
• Example:
let num = 123;
console.log(String(num)); // Output: "123"
20.Date():
• Definition: Creates a new Date object.
• Syntax: new Date()
• Example:
let currentDate = new Date();
console.log(currentDate); // Output: current date and time