Code that will export html table to excel
To quickly convert a number to a string, we can use the concatenation operator (+) plus followed by an empty set of quotation marks "" .
let val = 1 + "";
console.log(val); // Result: "1"
console.log(typeof val); // Result: "string"
The opposite can be quickly achieved using the addition operator + .
let int = "15";
int = +int;
console.log(int); // Result: 15
console.log(typeof int); Result: "number"
This may also be used to convert Booleans to numbers, as below:
console.log(+true); // Return: 1
console.log(+false); // Return: 0
If you want to convert a float to an integer, you can use Math.floor() , Math.ceil() or Math.round() . But there is also a faster way to truncate a float to an integer using |, the bitwise OR operator.
console.log(23.9 | 0); // Result: 23
console.log(-23.9 | 0); // Result: -23
Lastly, you may have used JSON.stringify before, but did you realise it can also help indent your JSON for you?
The stringify() method takes two optional parameters: a replacer function, which you can use to filter the JSON that is displayed, and a space value.
The space value takes an integer for the number of spaces you want or a string (such as '\t' to insert tabs), and it can make it a lot easier to read fetched JSON data.
console.log(JSON.stringify({ alpha: 'A', beta: 'B' }, null, '\t'));
// Result:
// '{
// "alpha": A,
// "beta": B
// }'
window.location object has a bunch of utility methods and properties. We can get information about the protocol, host, port, domain, etc from the browser URLs using these properties and methods.
var args = window.location.search
let data = new URLSearchParams(location.search).get('reqId');
console.log(data)
var arr = [10 , 20 , 30 ];
arr.length = 0; // arr will be equal to [].
Like the previous example of emptying an array, we truncate it using the length property.
var arr = [10, 20, 30, 40, 50, 60, 70, 80, 90];
arr.length = 4; // arr will be equal to [10, 20, 30, 40].
As a bonus, if you set the array length to a higher value, the length will be changed and new items will be added with undefined as a value. The array length is not a read only property.
arr.length = 10; // the new array length is 10
arr[arr.length - 1] ; // arr [10, 20, 30, 40, 50, 60, 70, 80, 90, undefined];
var foo = 10;
foo == 10 && doSomething(); // is the same thing as if (foo == 10) doSomething();
foo == 5 || doSomething(); // is the same thing as if (foo != 5) doSomething();
var a = 10;
var b = ( a++, 99 );
console.log(a); // a will be equal to 10
console.log(b); // b is equal to 99
let v1 = 'value 1';
let v2 = 'value 2';
[v1,v2] = [v2, v1];
console.log(v1, v2);
* | All elements. |
element | Basic selector defined by the elements name. IE – the <p> tag. |
.class | Elements specified by a specific class attribute. |
#id | Elements specified by the specific ID number. |
selector1, selector2 | Elements that match more than one selector. |
:first | First element from the selection. |
:last | Last element from the selection. |
:even | Even index selection. |
:odd | Odd index selection. |
:not(selector) | Everything except the selector in parentheses. |
:eq() | Elements with an index number equal to the parameter. |
:gt() | Elements with an index number greater than the parameter. |
:lt() | Elements with an index number less than the parameter. |
:header | All h1 – h6 HTML tags. |
:animated | Any element being animated. |
:focus | The element which has focus (an input item such as a field, checkbox, button, or link). |
:empty | Elements with no children. |
:parent | Elements that have a child node. |
:has(selector) | Elements contain atleast one of the elements that match the selector in parentheses. |
:contains(‘text’) | Elements that contain the specified text in parentheses. |
:hidden | Elements that are hidden. |
:visible | Elements that exist and take up space in the layout of the page. |
:nth-child(exp) | Elements that match the mathematical pattern. IE – div :nth-child(2+1) |
:first-child | First child from the current selection. |
:last-child | Last child from the current selection. |
:only-child | The only existing child of the matching element. |
[attribute] | Elements that have the specified attribute value – IE – [name^ = “value”] |
:input | Elements with input. |
:text | Elements with text. |
:password | Elements with password inputs. |
:radio | Elements with radio buttons. |
:checkbox | Elements with checkboxes. |
:submit | Elements with submit buttons. |
:image | Elements with images. |
:reset | Elements with reset buttons. |
:button | All buttons. |
:file | Elements with file inputs. |
:selected | All items from drop-down lists. |
:enabled | All enables elements, including defaults. |
:disabled | All disables elements. |
:checked | All checked checkboxes or radio buttons. |
If there’s anything that I’ve missed or more explanation that you’d like to see, feel free to leave a comment.
Help other's to help yourself !!