Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the '#' sign mean in JavaScript?

Tags:

javascript

I'm new to Javascript and I'm try to understand some code. I don't understand and I can't find any documentation about the # sign.

$(function () {
      $("#searchTerm").autocomplete({

What does $("#searchTerm") mean?

like image 818
Bastien Vandamme Avatar asked Jun 05 '12 14:06

Bastien Vandamme


2 Answers

Now # would/could mean private instance fields: https://tc39.github.io/proposal-class-fields/

2021 update:

Class fields are public by default, but private class members can be created by using a hash # prefix. The privacy encapsulation of these class features is enforced by JavaScript itself.

class ClassWithPrivateField {
  #privateField;
}

class ClassWithPrivateMethod {
  #privateMethod() {
    return 'hello world';
  }
}

class ClassWithPrivateStaticField {
  static #PRIVATE_STATIC_FIELD;
}

class ClassWithPrivateStaticMethod {
  static #privateStaticMethod() {
    return 'hello world';
  }
}

source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields

like image 90
celsowm Avatar answered Sep 23 '22 13:09

celsowm


In JavaScript? Nothing special. It is just part of a string.

The $ function might do something with it, but it is hard to tell what the $ function is.

There are a lot of libraries which provide a $ function that acts as a kitchen sink for that library. They include Prototype, Mootools and jQuery. This one looks most like jQuery, in which case the argument is a string containing a CSS selector, so the # indicates the start of an id selector.

This "Selects a single element with the given id attribute".

like image 28
Quentin Avatar answered Sep 21 '22 13:09

Quentin