0

JavaScript has 7 primitive data types: string, number, boolean, null, undefined, symbol, bigint

I understand the three data types, string, number, boolean; I also understand bigint is introduced since number is not enough. One example why number is simple floating number:

var a = 1.000000000000001;
var b = 1.0000000000000001;
console.log(Number.isInteger(a)) //false
console.log(Number.isInteger(b))  // true

below are my questions:

  1. What is undefined? Is it the same as void in c++? but void is a keyword. why undefined is a primitive data type in Javascript?
  2. why null a data type? seems typeof null return is an object. what the difference between undefined and null? what the similar thing in c++?
  3. what is symbol? I read this link, What is the motivation for bringing Symbols to ES6?, can someone give me an example in which we really need a symbol?
cso
  • 91
  • 9
  • 1
    **Do not** map one language's(javascript) features to another language's(C++) features. Undefined is nothing counterpart in c++. Just learn it with a fresh mind. – fangzhzh Jun 06 '19 at 00:56
  • 1.javascript is an interpreted language, an indefinite variable is a variable that has not been declared when it is used in its interpretation. – Mister Jojo Jun 06 '19 at 00:57
  • 2. javascript is an object language, it means that each variable is also treated as an object, and is therefore referenced by a pointer. If this pointer is null, it means that there is no instance of this object – Mister Jojo Jun 06 '19 at 01:00
  • @MisterJojo—your first comment is not helpful. *undefined* is a value, it has nothing to do with undeclared variables. Your second comment is also misleading, *null* is a value very similar to *undefined* but allows nuance as they can be interpreted differently. – RobG Jun 06 '19 at 01:03
  • @ Mister Jojo , so in compiled language, there be an error when compile, but in interpreted language, it has to use undefined? – cso Jun 06 '19 at 01:03
  • See frangzhzh's comment. *undefined* is a value that is assigned to things that need a value but one hasn't been assigned yet. It can also be specifically assigned. *null* is a nothing value that can be used to say "this variable/property/whatever has been deliberately assigned a nothing value". – RobG Jun 06 '19 at 01:06
  • @cso yes, in compiled language, *undefined* should make an error during compilation time – Mister Jojo Jun 06 '19 at 01:09
  • @RobG, if undefined and null are values, what is their types? a value should have a type, right? – cso Jun 06 '19 at 01:09
  • @ Mister Jojo, for null, I disagree you comment, cause if null is a primitive data type, it cannot be a object, it can not be both. – cso Jun 06 '19 at 01:13
  • [*undefined*](http://ecma-international.org/ecma-262/9.0/#sec-ecmascript-language-types-undefined-type) is a type that has one value, *undefined*. It's the only value that has that type. Same for [*null*](http://ecma-international.org/ecma-262/9.0/#sec-ecmascript-language-types-null-type). [*Types*](http://ecma-international.org/ecma-262/9.0/#sec-ecmascript-language-types) aren't very helpful in ECMAScript, `typeof x == 'undefined'` is used to avoid errors if *x* doesn't exist. – RobG Jun 06 '19 at 01:17
  • @RobG where did you see null is a primitive data type in javascript ? – Mister Jojo Jun 06 '19 at 01:19
  • ["*A primitive value is a member of one of the following built-in types: Undefined, Null, Boolean, Number, String, and Symbol*](http://ecma-international.org/ecma-262/9.0/#sec-ecmascript-overview). Types are just Types, you shouldn't try to infer too much from them or try to apply them too strictly. Some are endlessly puzzled for why there is both *null* and *undefined*. Just accept that they are and move on. :-) – RobG Jun 06 '19 at 01:55
  • O,k, and in JS, the fact is you can set any existing object to null ( for the garbage collector) or you can declare any variable to null and later set them by any create object – Mister Jojo Jun 06 '19 at 02:03
  • You can assign values to variables and object properties. An object is a value. If you create an object and assign it to a variable (e.g. `var a = {}`) then later you assign another value (e.g. `a = null`), if nothing else references the object it's available for garbage collection. The values are garbage collected, not the variable itself. They exist as long as the scope and execution context they are created in exists, they can't be extinguished, deleted, whatever. ;-) – RobG Jun 06 '19 at 02:10

1 Answers1

0

Basically, in javascript we have two categories of types: Primitives and Objects(functions and array are also objects.). Primitives are copied by their values, objects are copied by their reference.

Undefined and void in c++ are not same. Javascript also has void operator, void operator in javascript and c++ is same, but comparing javascript properties to C++ is not something reasonable. C++ vs Java is more reasonable.

differences between null and undefined

1-

let name; 

this is undefined, we declared but did not return any value. A function returns undefined if a value is not returned We can also explicitly set this to undefined.

let name=null,

We use null in situations when we wanna clear the value of the variable.

2-we can use null in JSON but we cannot use undefined in JSON.

3-undefined is a type, while null is an empty object. Why null is an object, something needs to be solved by ECMA. It is a bug in javascript. Even the creator of javascript accepted that it was a mistake.

We use SYMBOLS to make properties or methods of an object private. So we hide the details and show only the essentials. It is called abstraction in javascript.

How to implement this:Let's create a simple class with "radius" property

class Circle {
    constructor(radius) {
        this.radius = radius; 
    }
    }

A symbol is essentially a unique identifier. Every time we call this function we get a unique identifier. It is not a constructor function,though.

Symbol()===Symbol() //will be false

Implementation:

const _radius=Symbol()
class Circle {
    constructor(radius) {
    this[_radius] = radius; //since property name starts with _, we use bracket notation
        }
         }

now test this. Create an instance of Circle:

const c=new Circle;
console.log(Object.getOwnPropertyNames(c))// you will see a number on the console.
Yilmaz
  • 35,338
  • 10
  • 157
  • 202