JavaScript How can I declare a variable called variable - javascript

I was just wondering how to declare a variable called variable in JavaScript.
Or a variable called function.
Of course, its not necessary
Is it possible?
Just wondering.
Like this?
var var = "some string"
(you probably can't with javascript but any other language where it's possible?)

No. Refer to a list of reserved words - it's not only function and var.
As for your second question in an edit: for example, there's special syntax in C# to declare variables with reserved names (What does placing a # in front of a C# variable name do?).

As Nikita said, there are a bunch of reserved words you can't use like var and function. What (I think) you can do is put capitals like 'vAr' or 'functioN' but that's about as close as you can get to the actual word.

in MySQL you can use reserved word if you quote them like:
C
CREATE TABLE interval (begin INT, end INT); //this can't
CREATE TABLE `interval` (begin INT, end INT); //this can

Related

Is there any difference/advantage of using var before a variable name in javascript?

I am learning javascript . And I noted that the instructor was making variables in two ways .
first was (for example):
var name ="Any string here";
and second method was simply writing the variable name without writing var first :
name="Any string here";
And the result was same .
So , is there any difference between these two ?. Which one is better to use ?
Leaving out var makes it a global variable, so if you have name multiple places in the code, they'll overwrite each other.
First of all, it is a bad practice to not write var before variable
declaration.
Second problem is a global declaration of variable without var.
Third, in
strict mode using variable without declaration will cause Exception

Declaring variables javascript [duplicate]

This question already has answers here:
Why would a JavaScript variable start with a dollar sign? [duplicate]
(16 answers)
Closed 9 years ago.
I just have a quick question and cant find anything on google. I was going through some code another programmer put together and he declares ALL of his javascript variables with $ in front of them...for instance:
var $secondary;
Is there a reason for this? Could this cause problems in the future if JQuery ever ends up being used. I'm just curious because I was going to clean it up if so.
Is there a reason for this?
Hard to say. Maybe he came from a PHP background where $ prefixes the variables. Maybe he's a jQuery addict. Who knows? You'd have to ask him. That aside, $ is a perfectly legitimate character to use in a JavaScript variable name but as you noted, it could cause issues with jQuery. But that's why jQuery offers a noConflict() option.
I use this convention too keep track of if a variable is storing a JQuery object. So say the function getJQueryObject() returns a JQuery object and I want to store it.
i.e:
var $myJQobj = getJQueryObject();
Makes it clear that $myJQobj is a JQuery object unlike i.e
var myStr = "hello";
The $ as the first character in the identifier doesn't have any special meaning, you aren't invoking a method like $(), it's just a perfectly valid identifier in JavaScript. But the factthat the $ is used in JQuery makes what I was talking about before even clearer.
$ is a valid variable character, and in PHP all variables start with it. It's possibe that that particular developer uses $ as a "flag" to mean "this is a variable". It has no special meaning.
$ just a character that you can use in a variable name. Some people like to use it to denote variables that contain jQuery objects:
var $foo = $('#foo');
var bar = 42;
But that's just a personal preference. It has no special meaning.
its just a convention for jQuery DOM selctions.
var $logo = $('a.logo');
it wont cause any issues - it just lets other devs know that you're working with a jQuery wrapped dom element.
$ is fine for use in JavaScript. PHP uses the same variable syntax so maybe he was used to it from that.

unique preceding namespace characters for JS variables to handle scope

In different languages, you can have different characters in the variable names, and they also limit what can be the first character vs other characters as being legal.
In JS, what are these characters that are NOT used by other libraries? so that You can safely name a variable and nested ones that link to the global variable as you go down the rabbit hole in scope.
I am currently using underscore.js, so preceding a variable with _ can/could be problematic. Same goes with JQuery, and using $. I am getting errors if I try a ruby identifier such as # or ~, and I dont want to use . or # to avoid confusion with CSS selectors.
What do you know of (or suggest) to precede the variable names with to avoid confusion and overloading.
I would love a/several unique "golden" character(s), but realize this may be dependent on the stacks of libraries the project is using, so for this current project I am using: require, d3, jquery, underscore, backbone, text, dispatch.
sample code:
// for each measure in measuresCollection
_.each(this.measuresCollection.models, function(measure, index) {
var `this = this;
for (i=0; i<length; i++){
// ... some code with new preceding variable name `this
}

Why use $ (dollar sign) in the name of javascript variables? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Why would a javascript variable start with a dollar sign?
JQuery : What is the difference between “var test” and “var $test”
What is the difference between this two ways of initializing variables?
var $val = 'something'
OR
var val = 'something'
as I see they are the same thing.
Maybe in this case $ is only the part of name in variable?
(it will become a meaningless question in that case:/)
Thanks
The $ in the variable name is only part of the name, but the convention is to use it to start variable names when the variable represents a jQuery object.
var $myHeaderDiv = $('#header');
var myHeaderDiv = document.getElementById('header');
Now later in your code, you know the $myHeaderDiv is already a jQuery object, so you can call jQuery functions:
$myHeaderDiv.fade();
To get from the DOM-variable to the jQuery variable:
var $myHeaderDiv = jQuery(myHeaderDiv); //assign to another variable
jQuery(myHeaderDiv).fade(); //use directly
//or, as the $ is aliased to the jQuery object if you don't specify otherwise:
var $myHeaderDiv = jQuery(myHeaderDiv); //assign
$(myHeaderDiv).fade(); //use
To get from the jQuery variable to the DOM-variable.
var myHeaderDiv = $myHeaderDiv.get(0);
You are correct. $ is a part of the name of the variable.
This is not perl or PHP :)
No real difference..
It is usally used to signify a variable holding a jquery or other javascript framework object, because they can have shorthand $ function..
It is just easier to identify the type of the contents..
There are 28 letters in the alphabet as far as JavaScript is concerned. a-z, _ and $. Anywhere you can use a letter in JavaScript you can use $ as that letter. (<c> Fellgall # http://www.webdeveloper.com/forum/showthread.php?t=186546)
In your example $val and val will be two different variable names.
syom - in my case, i use the $ prefix to indicate that it's a variable that is referenced by jquery. It's purely a part of the variable and not a reserved character.
keeps it easy to identify in long code runs..
jim

correct function parameters designation

Every time i pass some parameters to a JavasScript or jQuery functon, i use some random letters. What are the correct letters for the corresponding variable types?
function(o){} for example is for a object. But what are the other letters? Do someone have a list of those?
I advise you not to assign names according to types but according to the data that the variable contain. If you pass an object that contains the configuration for a function call the variable configuration if you pass a variable that contains a name call it name and so on. In this way the code is more readable and understandable.
Any letter will do, there is no such thing as a standard for using letters as parameters.
However, using letters for parameters is bad practise, because a letter does not describe what the parameter is intended for.
Consider this example:
function ShowBusyCursor(s) {
//...
}
If you look at the function header, you cannot guess what the parameter s stands for. But if you change the code to this:
function ShowBusyCursor(cursorIsDisplayed) {
//...
}
The parameter cursorIsDisplayed is a lot more developer-friendly and tells you it's a boolean that can be set to true to display the cursor. If you used s, you would have to look into the code to figure that out.
Here is a good list of letters I could think of:
o - object
a - array
s - string
d - date
n - number
r - regexp
b - boolean
But seriously, jokes apart, I think you might be mixing up the packed/minified source with the actual source that developers write. For example, this little function looked like this originally:
function myFunction(personName) {
this.name = personName;
}
But after minifying, it becomes:
function myFunction(a){this.name=a}
packer is one such utility by Dean Edwards that will minify your Javascript. Play with your source code at this website and see the compressed output. Check the Shrink variables option on the right to see how parameters and other variables get renamed.
This however shouldn't affect how you write your code at all. Be it Javascript, or any other language, all identifiers - variables, function names, class names, etc. should be named after what they represent or do rather than obscure shorthands.
Re-iterating what mck89 said, it's better to go with explanatory variable names rather than just a letter for the type. What if you have more than one parameter of the same time, do you start appending numbers?
I have often seen explanatory variable names which include the type, for instance sFirstName would be a string denoted by the "s", bForce would be a boolean, oConfig would be an object and nSubTotal would be a number.
This is a variation of Hungarian notation, and I'd advise against using single-letter variable names. What if you need to pass in 2 of any one data type? Like others have said, it also doesn't represent the meaning of the variable very well. I think this is much better:
function foo(sFirstName, sLastName)
{
alert("Hi, my name is " + sFirstName + " " + sLastName);
}

Categories