What is the difference between a timeout and an interval in Javascript?

setTimeout( ) Method

The setTimeout() method sets a timer which executes a function or specified piece of code once after the timer expires. The function is only executed once. It returns a positive integer value which identifies the timer created by the call to setTimeout(); this value can be passed to clearTimeout() to cancel the timeout.

Syntax: –
setTimeout (function, milliseconds, para1, para2);
Ex: – var timeoutID = setTimeout(show, 2000);

clearTimeout( ) Method

The clearTimeout() method cancels a timeout previously established by calling setTimeout(). The ID value returned by setTimeout() is used as the parameter for the clearTimeout() method.

Syntax: – clearTimeout (timeoutID);

Ex: – clearTimeout(timeoutID);

setInterval( ) Method

The setInterval() method repeatedly calls a function or executes a code snippet, with a fixed time delay between each call. It returns an interval ID that uniquely identifies the interval, so you can remove it later by calling clearInterval().

Syntax: – setInterval (function, milliseconds, para1, para2);

Ex: – var intervalID = setInterval(show, 2000);

clearInterval( ) Method

The clearInterval() method cancels a timed, repeating action that was previously established by a call to setInterval().

Syntax: – clearInterval (intervalID);

Ex: – clearInterval(intervalID);

Tagged : / / / / / /

How does constructor work in JavaScript?

Constructor

Object instance are created with constructor, which is basically a special function that prepares a new instance of an object for use.

Constructor with Parameter

Tagged : / / /

How does this javascript factorial function work?

Factory Function

When a function returns an object, we call it a factory function. It can produce object instance without new keywords or classes.

Ex:-

Factory Function with Parameter

Ex:-

Tagged : / / /

Object Oriented Programming in JavaScript

Object Oriented Programming

Object-oriented programming (OOP) is a programming language model organized around objects rather than “actions” and data rather than logic.

Concepts of OOP

  • Encapsulation
  • Abstraction
  • Inheritance
  • Polymorphism

Objects

An object is a collection of properties, and a property is an association between a name (or key) and a value. A property’s value can be a function, in which case the property is known as a method. In addition to objects that are predefined in the browser, you can define your own objects.

Type of Objects

  • User-defined Objects – These are custom objects created by the programmer to bring structure and consistency to a particular programming task.
  • Native Objects – These are provided by the JavaScript language itself like String, Number, Boolean, Function, Date, Object, Array, Math, RegExp, Error as well as object that allow creation of user-defined objects and composite types.
  • Host Objects – These objects are not specified as part of the JavaScript language but that are supported by most host environments, typically browsers like window, navigator.
  • Document Objects – These are part of the Document Object Model (DOM), as defined by the W3C. These objects presents present the programmer with a structured interface to HTML and XML documents. Access to the document objects is provided by the browser via the document property of the window object (window.document).

Declaration and initialization of Object

  • Using Object Literal

Multiword key required quotation

Declaration and initialization of Object

  • Using Object Literal

Declaration and initialization of Object

  • Using Object Literal

Declaration and initialization of Object

  • Using Object Constructor

Declaration and initialization of Object

  • Using Object Constructor
Tagged : / /

What are arrow functions in JavaScript?

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};

Arrow Function with Parameter

•Default Parameter
Syntax: (para1, para2 = value) => {statement};
•Rest Parameter
Syntax: (para1, …args) => {statement};

Tagged : / / /

How do I use a default parameter in JavaScript?

Default Parameter

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).

Tagged : / / / /

How to Write Function in JavaScript?

Function

Function are subprograms which are used to compute a value or perform a task.

Type of Function

Library or Built-in functions

Ex: – valueOf( ) , write( ), alert( ) etc

– User-defined functions

Creating and Calling a Function

Creating a Function

Syntax: –

function function_name( )
{
Block of statement;
}

Calling a Function

Syntax: –
function_name( );

Ex: –
display( );

Rules of Function

  • Function name only starts with a letter, an underscore ( _ ).
  • Function name cannot start with a number.
  • Do not use reserved keywords. e.g. else, if etc.
  • Function names are case-sensitive in JavaScript.

How Function Call Works

The code inside a function isn‟t executed until that function is called.

Function with Parameters

Syntax: –
function function_name (parameter1, parameter2, ….)
{
Block of statement;
}

  • JavaScript function definitions do not specify data types for parameters.
  • JavaScript functions do not perform type checking on the passed arguments.
  • JavaScript functions do not check the number of arguments received.

Call Function with Parameter

Syntax: –
function function_name (para1, para2, ….)
{
Block of statement;
}
Syntax:-
function_name(argument1, argument2);

Function Argument Missing

If a function is called with missing arguments, the missing values are set to undefined.

Arguments Object

The arguments object contains an array of the arguments used when the function was called.
This object contains an entry for each argument passed to the function, the first entry’s index starting at 0. The argument’s object is not an Array. It is similar to an Array but does not have any Array properties except length.

Many Function Arguments

If a function is called with too many arguments, these arguments can be reached using the arguments object which is a built-in.

Tagged : / / /

Tutorials for PHP Function with Parameters or Arguments

What is Function with Parameters or Arguments?

The information or variables included within the function’s parentheses are referred to as parameters. These are used to keep track of values that can be used during runtime. The comma(,) operator allows the user to input as many arguments as he wants. These options are used to accept inputs during runtime. When transferring numbers, such as during a function call, arguments are used. A parameter is a container for parameters that are given to a function. A value supplied to a function is referred to as an argument. In layman’s terms, both parameter and statement have the same meaning. It’s crucial to keep in mind that each parameter must be accompanied with an argument.

PHP functions,Parameters and arguments

Syntex:-

function function_name($first_parameter, $second_parameter) {
    executable code;
}

Example:-

Setting Default Values for Function parameter:-

In PHP, we may define default arguments for function parameters. If no argument is supplied for a parameter with a default value, PHP will call the function with the parameter’s default specified value.

The default value for the parameter $num in the previous example is 12; if no value is supplied for this parameter in a function call, the default value is used. It’s also required because the option $str has no default value.

Functions with return values:-

Functions can also return values to the portion of the program where they were called. The value is returned to the program section from where it was named using the return keyword. It is possible to return any type of value, including arrays and objects. The return statement also marks the conclusion of the function, stops it from running, and returns the value.

Parameter passing to Functions:-

In PHP, there are two ways to pass an argument to a function.

Pass by Value:– Moving by value changes the value of an argument within a function, while the original value outside the function stays unaffected. This means that a statement that is a duplicate of the original value is transmitted.

Pass by Reference:– As statements are passed via proxy, the original value is transmitted. As a result, the starting value has changed. Using the ampersand symbol in pass by reference, we just transmit the value’s address, where it is kept (&).

Example:-

Tagged : / / / / / / / / / / / / / /

Understand Shell Script Parameters – Reference

shell-script-parameters

A parameter is an entity that stores values. It can be a name, a number or some special characters.

Bash shell provides two kind of parameters.

Positional Parameter and Special Parameter

Bash Positional Parameter – $0, $1, $2 ..

Positional parameters are the arguments given to your scripts when it is invoked. It could be from $1 to $N. When N consists of more than a single digit, it must be enclosed in a braces like ${N}.

The variable $0 is the basename of the program as it was called.

The following example gets two arguments and provides arithmetic operations result between those two integers.

First, create the arithmetic.sh shell script as shown below.

$ cat arithmetic.sh

#!/bin/bash

echo -e “\$1=$1”

echo -e “\$2=$2”


let add=$1+$2

let sub=$1-$2

let mul=$1*$2

let div=$1/$2

echo -e “Addition=$add\nSubtraction=$sub\nMultiplication=$mul\nDivision=$div\n”

Next, execute the arithmetic.sh with proper parameters as shown below.

$ ./arithmetic.sh 12 10

$1=12

$2=10

Addition=22

Subtraction=2

Multiplication=120

Division=1

In the above output $1 has the value 12, and $2 has 10.

Shell built-in ‘let’ allows arithmetic operation to be performed on shell variables. The above script does the arithmetic operations such as addition, subtraction, multiplication and division on the given parameters.

Set / Unset Bash Positional Parameters

The built in set command is used to set and unset the positional parameter.

First, create the positional.sh shell script as shown below.

$ cat positional.sh

#!/bin/bash

# From command line

echo -e “Basename=$0”

echo -e “\$1=$1”

echo -e “\$2=$2”

echo -e “\$3=$3”

# From Set builtin

set First Second Third

echo -e “\$1=$1”

echo -e “\$2=$2”

echo -e “\$3=$3”

# Store positional parameters with -(hyphen)

set – -f -s -t

echo -e “\$1=$1”

echo -e “\$2=$2”

echo -e “\$3=$3”

# Unset positional parameter

set —

echo -e “\$1=$1”

echo -e “\$2=$2”

echo -e “\$3=$3”

The above script prints the command line arguments first, then set command sets the positional parameter explicitly. Set with the – refers end of options, all following arguments are positional parameter even they can begin with ‘-’. Set with ‘–’ without any other arguments unset all the positional parameters.

Next, execute the positional.sh as shown below.

$ ./positional.sh

Basename=positional.sh

$1=12

$2=10

$3=

$1=First

$2=Second

$3=Third

$1=-f

$2=-s

$3=-t

$1=

$2=

$3=

Use Bash $* and $@ to Expand Positional Parameters

This example shows the value available in $* and $@.

First, create the expan.sh as shown below.

$ cat expan.sh

#!/bin/bash

export IFS=’-‘

cnt=1

# Printing the data available in $*

echo “Values of \”\$*\”:”

for arg in “$*”

do

echo “Arg #$cnt= $arg”

let “cnt+=1”

done

cnt=1

# Printing the data available in $@

echo “Values of \”\$@\”:”

for arg in “$@”

do

echo “Arg #$cnt= $arg”

let “cnt+=1”

done

Next, execute the expan.sh as shown below to see how $* and $@ works.

$ ./expan.sh “This is” 2 3

Values of “$*”:

Arg #1= This is-2-3

Values of “$@”:

Arg #1= This is

Arg #2= 2

Arg #3= 3

· The above script exported the value of IFS (Internal Field Separator) with the ‘-’.

· There are three parameter passed to the script expan.sh $1=”This is”,$2=”2″ and $3=”3″.

· When printing the each value of special parameter “$*”, it gives only one value which is the whole positional parameter delimited by IFS.

· Whereas “$@” gives you each parameter as a separate word.

Use $# to Count Positional Parameters

$# is the special parameter in bash which gives you the number of positional parameter in decimal.

First, create the arithmetic.sh as shown below.

$ cat arithmetic.sh

#!/bin/bash

if [ $# -lt 2 ]

then

echo “Usage: $0 arg1 arg2”

exit

fi

echo -e “\$1=$1”

echo -e “\$2=$2”

let add=$1+$2

let sub=$1-$2

let mul=$1*$2

let div=$1/$2

echo -e “Addition=$add\nSubtraction=$sub\nMultiplication=$mul\nDivision=$div\n”

If the number of positional parameters is less than 2, it will throw the usage information as shown below,

$ ./arithemetic.sh 10

Usage: ./arithemetic.sh arg1 arg2

Process related Parameters – $$ and $!

The special parameter $$ will give the process ID of the shell. $! gives you the process id of the most recently executed background process.

The following script prints the process id of the shell and last execute background process ID.

$ cat proc.sh

#!/bin/bash

echo -e “Process ID=$$”

sleep 1000 &

echo -e “Background Process ID=$!”

Now, execute the above script, and check the process id which its printing.

$ ./proc.sh

Process ID=9502

Background Process ID=9503

$ ps

PID TTY TIME CMD

5970 pts/1 00:00:00 bash

9503 pts/1 00:00:00 sleep

9504 pts/1 00:00:00 ps

$

Other Bash Special Parameters – $?, $-, $_

· $? Gives the exit status of the most recently executed command.

· $- Options set using set builtin command

· $_ Gives the last argument to the previous command. At the shell startup, it gives the absolute filename of the shell script being executed.

$ cat others.sh

#!/bin/bash

echo -e “$_”; ## Absolute name of the file which is being executed

/usr/local/bin/dbhome # execute the command.

#check the exit status of dbhome

if [ “$?” -ne “0” ]; then

echo “Sorry, Command execution failed !”

fi

echo -e “$-“; #Set options – hB

echo -e $_ # Last argument of the previous command.

In the above script, the last echo statement “echo -e $_” ($ underscore) also prints hB which is the value of last argument of the previous command. So $_ will give the value after expansion

$ ./others.sh

./others.sh

/home/oracle

Sorry, Command execution failed !

hB

hB

Tagged : / / / / / / / / / / / / / / / /