Categories
JavaScript Answers

How to Display Only 10 Characters of a Long String in JavaScript?

To display only 10 characters of a long string in JavaScript, we can use the slice method to return the first 10 characters of the string.

For instance, we can write:

const text = "I am too long string";  
const count = 10;  
const result = text.slice(0, count) + (text.length > count ? "..." : "");  
console.log(result)

We have the text string variable.

And then we have the count set to the max number of characters we want in the truncated string.

Next, we create the result truncated string by calling slice with 0 and the count .

And then we add '...' to the end of the string if text.length is bigger than count .

Categories
JavaScript Answers

How to Find the Closest Element to a Parent Element in an HTML Document without jQuery?

To find the closest parent element to an HTML element in an HTML document without jQuery, we can use the closest method to find the closest parent of the given element.

For instance, if we have the following HTML:

<table>  
  <thead>  
    <tr>  
      <th>head</th>  
    </tr>  
  </thead>  
  <tbody></tbody>  
</table>

Then we can write:

const th = document.querySelector('th')  
console.log(th.closest('thead'));

to get the th element with document.querySelector .

Then we call closest on it with the 'thead' string to get the thead element closest to the th element.

The console log should log the thead element as a result.

Categories
JavaScript Answers

How to Check if a Div Does Not Exist with JavaScript?

To check if a div does not exist with JavaScript, we can check if the document.getElementById or document.querySelector returns a null value.

For instance, we can write:

if (!document.getElementById("given-id")) {
  console.log('not exist')
}

if (!document.querySelector("#given-id")) {
  console.log('not exist')
}

We have 2 if statements.

The first one calls document.getElementById to check if an element with the ID given-id exists.

The 2nd one calls document.querySelector to do the same thing.

They both log 'not exist' since they don’t exist.

Also, we can write:

if (document.getElementById("given-id") === null) {
  console.log('not exist')
}

if (document.querySelector("#given-id") === null) {
  console.log('not exist')
}

to do the same thing by checking explicitly if null is returned.

We should get the same result as before the elements with ID given-id doesn’t exist.

Categories
JavaScript Answers

How to Convert an Integer Array into a String Array in JavaScript?

Sometimes, we want to convert an integer array to a string array in JavaScript.

In this article, we’ll look at how to convert an integer array to a string array in JavaScript.

Convert an Integer Array into a String Array in JavaScript

To convert an integer array to a string array in JavaScript, we can use the map method to do so.

For instance, we can write:

const arr = [1, 2, 3, 4, 5];
const strArr = arr.map(String)
console.log(strArr)

to create the arr number array.

Then we call map on arr with the String function as its callback.

The String function takes in the item we want to convert into a string and return the string version of the argument as a result.

Therefore strArr is:

["1", "2", "3", "4", "5"]

Conclusion

To convert an integer array to a string array in JavaScript, we can use the map method to do so.

Categories
JavaScript Answers

How to Check if Two JavaScript Dates Have the Same Date Info?

To check if two JavaScript dates have the same date info, we can use the valueOf or getTime method to return the timestamp value of both dates, which are numbers.

Then we can compare the returned timestamp numbers with === .

For instance, we can write:

const a = new Date(1995, 11, 17);  
const b = new Date(1995, 11, 17);  
console.log(a.getTime() === b.getTime())

Then the console log logs true since a and b have the same timestamp number.

Also, we can use valueOf by writing:

const a = new Date(1995, 11, 17);  
const b = new Date(1995, 11, 17);  
console.log(a.valueOf() === b.valueOf())

And we get the same result as the previous example.