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 : / / / /

CSS Overflow: What It Is & How It Works?

Overflow

This property is used to define what happens if content overflows an element’s box. It means it will define whether to clip content or to add scrollbars when an element’s content is too big to fit in a specified area. This property only works for block elements with a specified height. We can set this property to visible (default), scroll, hidden, auto.

Ex:-

p { overflow: hidden; }

Diagnosis: overflow:hidden; (submenu truncated by theme CSS) - UberMenu  Troubleshooter Diagnosis

Overflow-x

This property is used to define whether to clip content, render a scroll bar, or display overflow content of a block-level element when it overflows at the left and right edges. We can set this property to visible (default), hidden, scroll, auto.

Ex: –

p { overflow-x: hidden; }

Overflow-y

This property is used to define whether to clip content, render a scroll bar, or display overflow content of a block-level element when it overflows at the top and bottom edges. We can set this property to visible (default), hidden, scroll, auto.

Ex: –

p { overflow-y: hidden; }

overflow-x hidden doesn´t let my content that is inside go out - Stack  Overflow
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 : / / /

How to set element Layer (z-index) in CSS?

z-index property is used to set the layer of elements. We can set this property to auto (default) and number. It works only with position property value: fixed, relative, absolute.

The element which has a higher number appears at the top.

Ex: –

img {
position: fixed;
z-index: 565;
}

What is Z-Index and How Does It Work? - SpyreStudios
Tagged : / / /

How to use the position property in CSS to align elements?

In This tutorial, I am going to explain that how to How to use the position property in CSS to align elements? Below Property to uses in align elements-

Position-

  • Left
  • Right
  • Bottom
  • Top
  • static
  • Fixed
  • Relative
  • Absolute

Length (cm, px, %)

Left Align Property-

  • left: auto
  • left: 55px
  • left: 50%

Right Align Property-

  • right: auto
  • right: 23px
  • right: 50%

Bottom Align Property-

  • bottom: auto
  • bottom: 23cm
  • bottom: 40%

Top Align Property-

  • top: auto
  • top: 20%
  • top: 40px

static

When we set static position for an element then it is not positioned in any special way, it is always positioned according to the normal flow of the page. left, right, top and bottom property won’t work with static position.

Ex: –

h1 {
position: static;
border: 5px solid red;
}

h1 {
position: static;
border: 5px solid red;
left: 50px;
}

fixed-

When we set fixed position for an element it always stays in the same place even if the page is scrolled. The top, right, bottom, and left properties are used to position the element.

img {
position: fixed;
right: 0;
top: 50%;
border: 3px solid red;
}

relative-

When we set relative position for an element, elements positioned to its normal flow.

h1 {
position: relative;
left: 50px;
border: 3px solid red;
}

absolute-

When we set absolute position for an element, element is positioned to the nearest positioned ancestor. If there is no positioned ancestor then it follow the normal position according to browser.

div {
position: absolute;
top: 80px;
right: 0;
width: 200px;
height: 100px;
border: 3px solid red;
}

Absolute Positioning Inside Relative Positioning | CSS-Tricks

These properties are used to position an element.

We can not use any of these properties without setting up position property.

Tagged : / / / / / / / /