ラベル javascript の投稿を表示しています。 すべての投稿を表示
ラベル javascript の投稿を表示しています。 すべての投稿を表示

2020年1月23日木曜日

javascript primitive string

Let's see below example

let s1 = '2 + 2'              // creates a string primitive
let s2 = new String('2 + 2')  // creates a String object
console.log(eval(s1))         // returns the number 4
console.log(eval(s2))         // returns the string "2 + 2"

String primitives and String objects

String primitives and String objects also give different results when using eval(). Primitives passed to eval are treated as source code; String objects are treated as all other objects are

ref

2020年1月22日水曜日

Javascript function, passing variable or value

let's see the below function

function update(o) {
  o.a = o.a + 1;
  o = { a: 100 };
}

and do the following test ,

>const obj = {a:1} 
>update(obj)

console.log(obj)

what is the result ?

it would be 2.

> console.log(obj.a)

1


note that
even if the object is const value, the object keys are not protected.

in the function update(o), first line of o.a is still seeing the value of the obj, which is out of the scope of the function,
then second line, o = {a:100} , which is replacing o in the scope of function to a new object, however obj  in the outside of the function is not changed.

Javascript NaN, comparing or not

NaN is not a number and would appear when you calculate number.

>const result = "foo" * 2;

it will give you NaN as result.

however this

>console.log(result === NaN);

it will print false

because

NaN compares unequal (via ==, !=, ===, and !==) to any other value -- including to another NaN value. Use Number.isNaN() or isNaN() to most clearly determine whether a value is NaN. Or perform a self-comparison: NaN, and only NaN, will compare unequal to itself.


>Number.isNaN(result)
>isNaN(result)

will return true


2020年1月18日土曜日

Javascript Boolean()

Boolean()
Creates Boolean objects.
The Boolean object is an object wrapper for a boolean value. (ref)


what do you think the output of the following code ?
const bool = "false";
console.log(Boolean(bool)); 

it will print true.

because Boolean() evaluate values which are nil, or undefined to false but other value for example String to true.

const bool = "false";
console.log(bool == false); // false

console.log(Boolean(bool)); // true

also an empty array [] would be evaluated to true

const bool = [];
console.log(Boolean(bool)); // true


Don't confuse Boolean() and boolean primitive like true or false

Custom reduce function to Javascript Allay class

learn how to use Javascript prototype.
let's make a reduce() function of  Allay class using  javascript prototype.


Array.prototype.myReduce = function(callback, initVal) { 
 let acc = initVal; 
  for (i = 0; i < this.length; i++) { 
   acc = callback(acc, this[i]); 
  }; 
 return acc
}


let myArray = [1,2,3]
myArray.myReduce((acc, elem) => { return acc + elem; }, 0)
6


What is reduce() function 

from mozira document of reduce

The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.

const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;

// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10

// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));

// expected output: 15