An arrow function expression (previously, and now incorrectly known as fat arrow function) has a shorter syntax compared to function expressions. Arrow functions are always anonymous.
Arrow Function
Do not call before definition
An arrow function cannot contain a line break between its parameters and its arrow.
Arrow Function With Parameter
•With one Parameter Syntax: (para) => {statement}; Syntax: para => {statement}; •More than one Parameter Syntax: (para1, para2, paraN) => {statement}; •No Parameter Syntax: ( ) => {statement};
Syntax: – function function_name (para1, para2=“value”, para3) // problem undefined { Block of statement; } Syntax: – function function_name (para1, para2=“value1”, para3=“value2”) { Block of statement; }
JavaScript also allows the use of arrays and null as default values.
Rest Parameters
The rest parameter allows representing an indefinite number of arguments as an array.
Syntax: – function function_name (…args) { Block of statement; }
Syntax: – function function_name (a, …args) { Block of statement; }
The rest operator must be the last parameter to a function.
Rest Vs Arguments
There are three main differences between the rest parameters and the arguments object:-
Rest parameters are only the ones that haven’t been given a separate name, while the arguments object contains all arguments passed to the function.
The arguments object is not a real array, while Rest Parameters are Array instances, meaning methods like sort, map, forEach or pop can be applied on it directly.
The arguments object has additional functionality specific to itself (like the callee property).
prompt( ) – The browser provides a built-in function that can be used to get input from the user, named prompt. The prompt( ) method displays a dialog box that prompts the visitor for input.
Once the prompt function obtains input from the user, it returns that input.
Syntax: – prompt(text, defaultText)
Ex:- prompt(“Enter Your Name: “, “name”); prompt(“Enter Your Roll No. : “);
A function is a segment of code in a program that is written to do a certain task. We should compare functions to workers in a real-world workplace to have a better grasp of how they work. Assume the management delegated the duty of determining the annual budget to one of his employees. So, what will be the outcome of this procedure? The employee would ask his supervisor for data, perform estimations, calculate the budget, and report the results to his superior. Functions work in a similar fashion. They accept data as an argument, process it using a set of statements or operations, and then return the output.
How many Types of Functions in PHP?
There are Two Types of Functions :-
User Defined Functions:-These capabilities are used by developers and programmers when they need to execute their own logic. This functions are specified with the keyword function, and when a function call is made, a sequence of statements will be written within the function to execute it. Simply type functionname() in the address bar, and the function will be run.
Built-in functions:- These routines offer us with already-built-in library functionality. The PHP installation kit includes several capabilities, making the language more efficient and helpful. To use the function’s attributes, all we have to do is call it when we want the desired outcome. PHP makes use of date, numeric, string, and other built-in functions.
String Functions: These PHP features include built-in support for working with strings. Some of the string methods in PHP include strpos(), strncmp(), strrev(), and strlen() ,
Date Function: The format is a human-readable UNIX date and time format, and these functions are PHP standard components.
Numeric Functions: These functions have their own preset logic for numeric operations that PHP provides. As a result, it will either return a Boolean or a numeric value. Among them are is number(), number format(), round(), and other numeric procedures.
Why we should Use Functions in PHP?
Reusability:- This programming language has a feature that reduces the amount of lines of code that must be written many times. This would save time and effort for the developer or coder. We can put a piece of code in a feature and call it whenever and wherever it’s needed if it has to be used in many locations. This may be done by calling the functions from other programmes or from inside the same package.
Easier Error Detection:- Because the code is written as functions rather than single blocks, any problems may be immediately and readily recognized and rectified.
Easily Maintained:- Because functions are used throughout the program, we may quickly modify any function or line of code in the function, and the change will be reflected. As a result, it is simple to maintain everywhere.
How to create functions in PHP?
A function is to find a collection of statements in a programmer repeatedly. When a function in a program is invoked, it is simply implemented. There are a few things to keep in mind while creating a user-identified feature.
Any word ending in an opened and closed parenthesis is a feature.
The function keyword is commonly used to start a name of the function.
Type your name followed by a parenthesis to launch a command.
The initial letter in a function word cannot be a number. It might start with a letter or an emphasis.
name.js – This is our script file, which we created. The code of javascript is written inside devops.js. In which DevOps is the file name, .js is the extension. Javascript has an extension of .js
Type – This is also an attribute of the script tag, it tells the browser that it is javascript. Now it is not necessary to write it, you can leave it if you want.
Text/javascript – It defines what type these documents are.
document.write(“Hello World”); – This is a function which is displaying the data.
</script> – Closing Script tag.
How to link more than one External?
You can link more than one JavaScript file to a single document. Just need to write different script tags for that.
This function is used to write arbitrary HTML and content into the page. If we use this function after an HTML document is fully loaded, will delete all existing HTML. It is used only for testing purposes.
Ex:- document.write("Hello world");
document.write(variable);
document.write(4+2);
document.write("hello world . <br>");
document.write("hello world . <br>" + variable +"<br>");
Alert function in JavaScript
The data that will be displayed in it will be in the dialog box. You forcefully want to see this thing, then only this work will be done. In that case, you use a window. alert
An identifier is a name having a few letters, numbers, and special characters _(underscore). It is used to identify a variable, function, symbolic constants, and so on.
Ex:- X2 PI Sigma Matadd
Veriable in JavaScript
Variable can be any name, in which a combination of letters, numbers, and special characters can be written _()(underscore) and $(dollar). It is not necessary that there should be only a combination, it can only be a letter.
Ex:- c, fact, b33, total_amount ect.
Rules of Veriable in JavaScript
Variable can contain a combination of letters, digits, _()(underscore), and dollar signs($).
Must begin with a letter A-Z or a-z or underscore or dollar sign.
What was easy in any language was made core and what became difficult was made an advance. What happens in Core is of basic level and what happens in Advance is of upper-level.
Object Oriented Progrmming
Object-Oriented Programming is a Technic that solves the problem. In this, the code breaks the object. And you see your problem on the side of the object. In which you find your problem similar to the real-world problem and it is easy to maintain, and it has to be solved.
Class
The PHP class has a group of values. To manipulate whatever value you write inside the class, you also write operations in it. The class gives you this facility, that if the user does not need to know the information. So you can also hide the information. To use the application, you can keep it hidden for security purposes. Or if it will be accessed from the outside users or from an outside viewer then your application may be damaged. That’s why you can keep your application hidden, it gives class facility. You can define type using class.
Rule
The class name can be any valid label.
It can’t be PHP reserved world.
The class name must start with a letter or an underscore. Following by any number of letters, numbers, or underscore all this will work. Lwkin should not start with number.
class Classname
{
var $variable_name; (It is also called Data Member/Properties or Attribute.)
var $variable_name;
function Method_name()
{
Body of method;
}
function Method_name(parameter_list) (And these methods are called methods or member functions.)
{
Body of Method;
}
}
See your problem in the real world.
class Mobile
{
public $model;
function show Model (Sumber)
{
$this->model = $number;
echo "Model Number: $this->model";
}
}
Object
The object is a variable of class data type. Whenever you create an object, for that object, whatever is proper inside the class, a copy of it is prepared and it is prepared for that object. If there are 2 objects then 2 copies of that variable or that property will be created and one copy for the first object and a second copy for the second object. Whatever object you make, its copy will be ready and one copy will be distributed. But this is not the case with the member function. The member function is the same, all the objects share with it.
The new Operator is used to create an object.
Syntax:
class Mobile
{
public $model;
function show Model (Sumber)
{
$this->model = $number;
echo "Model Number: $this->model";
}
}
$samsung = new Mobile;
$samsung->showModel('A5');
$lg = new Mobile;
$lg->showModel('M30');
You will not need to write the class, again and again, you just have to create an object of it. And call this method with the help of that object. It will show its own model number.
Important thing
A class is of no importance unless it has an object.
Accessing class member using object
How would you make accessing a member of a class into a user object?
-> This operator is used to access the member of the class.
object_name->variable_name; $samsung->model;
access to method Object_name->method_name(); $samsung->showModel();
in parameter Object_name->method_name(parameter_list) $samsung->showModel(‘A8’);
A JavaScript function is a block of code designed to perform a particular task. In simple terms, Functions are used to make work easy, reduce the effort of coding multiple lines for the same task which is needed to repeat multiple times.
Function Declaration
FunctionDefinition.
FunctionCall
In JavaScript function declaration and definition are done together.