DEV Community

Nnaji David
Nnaji David

Posted on

Variable explanation help:

You can help me explain some of this words just in variables.

-> Concatenation
-> conversion
-> Coercion
-> Truthy and Falsy .

and give me some examples too...
Note: This words are under variables types in javascript

Discussion (8)

Collapse
nombrekeff profile image
Keff

Concatenation:

const name = 'bob';
const lastName = 'sprinklebutt';

const fullName = name + ' ' + lastName; // Here, name is contatenated with lastName to create a new string
Enter fullscreen mode Exit fullscreen mode

Conversion:

const numString = '1234';
const num = Number(numString); // Convert string to a number
Enter fullscreen mode Exit fullscreen mode

Coercion:

const stringNumber = '12';
const num = 12;
const coerced = stringNumber + num; // Results in "1212"
true == 1; // true
Enter fullscreen mode Exit fullscreen mode

Type coercion is the process of converting value from one type to another (such as string to number, object to boolean, and so on). Any type, be it primitive or an object, is a valid subject for type coercion.
Exerpt from freecodecamp.org/news/js-type-coer...

Truthy and Falsy:
This is a weird one in JS

true == 'true' // true
true == 'false' // true
true == 1 // true
1 == 1 // true
1 == '1' // true
1 == '2' // false
[] == false // true
Enter fullscreen mode Exit fullscreen mode
Collapse
nombrekeff profile image
Keff

Hope it's useful

Collapse
callmebobonwa profile image
Nnaji David Author

thanks man for this,
i really appreciate.

Collapse
andrewbaisden profile image
Andrew Baisden

This is a great explanation right here.

Collapse
callmebobonwa profile image
Nnaji David Author

thanks for this,
very simple to understand

Collapse
callmebobonwa profile image
Nnaji David Author

wow,
thanks for this

Collapse
callmebobonwa profile image
Nnaji David Author

Thanks for this..

i really appreciate

Collapse
dovey21 profile image
Somtochukwu Nnaji • Edited on

Coercion happens when you try to join two different values (values like string and Number) together e.g
When you something like this

let age = 12;
let myName = "dovey";
const result=age + myName;
console.log(result)

the result would be 12dovey javascript engine noticed that they both are not of the same value and can not be joined together to get an appropriate mean so it had to coerced i.e forcefully joining them together to get a result.