Squaring Elements in Array

If I want to square every number in array, why does [1:10].^2 not work?

Apologies if this has been asked before - I have tried searching for similar threads.

Because

julia> [1:10]
1-element Array{UnitRange{Int64},1}:
 1:10

i.e. it’s an array containing a single UnitRange object (and there’s no way to square a vector). Try

julia> (1:10).^2
10-element Array{Int64,1}:
   1
   4
   9
  16
  25
  36
  49
  64
  81
 100
4 Likes

Thank you!