> On Dec 27, 2014, at 1:34 PM, Roelof Wobben wrote:
>
> Oke
>
> Let's say it a one and one logging purpose.
I don't understand.
>> var example = "example string"
>> console.log(example.length);
>>
>> Is this valid and good javascript
This is fine.
>> or can I better use a variable for the length and use that on console.log ?
If your JavaScript will run in an old web browser with a slow and/or poorly-optimized JavaScript engine (old versions of Internet Explorer, especially), and you will be using the length of the string repeatedly (in a loop, for example), then you can get slightly better performance by using an intermediate variable:
var example = "example string";
var exampleLength = example.length;
for (var i = 0; i < 10; ++i) {
console.log(exampleLength);
}
But don't worry about those kinds of micro-optimizations when first learning JavaScript, and don't worry about them at all for modern JavaScript engines like the v8 engine found in Google Chrome and in nodejs.
Using intermediate variables for properties you use in several places in code can also reduce the size of your code when minified, but that's another micro-optimization, and only applicable if the code will be sent over the wire to a web browser, and not applicable for server-side code running under nodejs for example.