Hello everyone, I have two questions on JS.

  1. What will the following record return to me: document.body.style.opacity ? Line or fractional number?
  2. How to convert one type to another in Javascript? What is the recording form?

Thank.

I experimented, alert (document.body.style.opacity); gives undefined. The question is why? because in the css style opacity is set.

    3 answers 3

    1. document.body.style usually implies a string. (why don't you experiment yourself?)
    2. Type (variable) (alert (Boolean (0));) some types also imply translation from one type to another, for example: "123" .parseInt ();

    You are welcome.

    PS: In general, the concept of types in a non-typed language is very vague.

    • I agree with you, I just thought about it, and what kind of return is he having, although I used to program it without that) - Andrei Arshinov

    document.body.style.opacity will return depending on the browser (different - differently), but mostly return a fractional number, in our case 0.1 or 1.0 . Javascript is not so deeply know, but I can suggest a function that pulls out integers from the string - parseInt () .

      1. If opacity is specified in the script or it is registered directly in style = "...", it will return the string. To convert to a fractional number, use parseFloat ().
      2. And why, if everything is already given? In principle, there are ways to reduce to other types:
        bool : !!param
        string : param+''
        int : parseInt(param)
        float : parseFloat(param)
        array : [param]