๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_variables.asp
JavaScript Variables
If you put a number in quotes, it will be treated as a text string. const pi = 3.14; let person = "John Doe"; let answer = 'Yes I am!'; Try it Yourself ยป ยท You can declare many variables in one statement. Start the statement with let or constand separate the variables by comma: let person = "John Doe", carName = "Volvo", price = 200; Try it Yourself ยป ... In JavaScript, the equal sign (=) is an assignment operator, not an equal to operator.
๐ŸŒ
freeCodeCamp
forum.freecodecamp.org โ€บ javascript
Basic JavaScript - Declare String Variables - JavaScript - The freeCodeCamp Forum
September 11, 2023 - Your code so far var darko = myFirstName ; var rudnik = myLastName ; Your browser information: User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Safari/605.1.15 Challenge: Basic JavaScript - Declare String Variables Link to the ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-declare-string-variables-in-javascript
How to declare String Variables in JavaScript?
JavaScript is an untyped language. This means that a JavaScript variable can hold a value of any data type. To declare variables in JavaScript, you need to use the var, let, or const keyword. Whether it is a string or a number, use the var, let, or c
๐ŸŒ
JavaScript.info
javascript.info โ€บ tutorial โ€บ the javascript language โ€บ javascript fundamentals
Variables
The statement below creates (in other words: declares) a variable with the name โ€œmessageโ€: ... The string is now saved into the memory area associated with the variable.
๐ŸŒ
Freewebdesigntutorials
freewebdesigntutorials.com โ€บ javaScriptTutorials โ€บ jsStringObject โ€บ declaringStrings.php
Declare a String in JavaScript
We can explicitly declare a string using "new String()" to create the string in memory: this is the JavaScript string constructor function. An optional string argument can also be passed to the constructor. // Declare string variable with string constructor var carter1 = new String("I learn ...
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Learn_web_development โ€บ Core โ€บ Scripting โ€บ Strings
Handling text โ€” strings in JavaScript - Learn web development | MDN
Template literals mostly behave the same as normal strings, but they have some special properties: You can embed JavaScript in them. You can declare template literals over multiple lines. Inside a template literal, you can wrap JavaScript variables or expressions inside ${ }, and the result will be included in the string:
๐ŸŒ
SheCodes
shecodes.io โ€บ athena โ€บ 8931-creating-a-string-with-variables-in-javascript
[JavaScript] - Creating a String With Variables in | SheCodes
Learn how to add a variable to a string using either string concatenation or string interpolation with template literals for a more modern approach.
๐ŸŒ
YouTube
youtube.com โ€บ watch
Declare String Variables (Basic JavaScript) freeCodeCamp tutorial - YouTube
Do you need more help with coding?โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โœ… Apply for 1-1 coaching https://form.jotform.com/230156286763056Certification: JavaScript Algo...
Published ย  May 21, 2021
Find elsewhere
Top answer
1 of 5
12

Don't use

var str = new String();

Because

var str = new String("dog");
var str2 = new String("dog"); 
str == str2; // false

But

var str = "dog";
var str2 = "dog"; 
str == str2; // true

However, because of type coercion, the following works (Thanks to Rocket for pointing it out)

var str = new String("dog");
var str2 = "dog"; 
str == str2; // true

Single and double quotes don't matter, except for what quotes need to be escaped. Many others have noted that single quotes are better when creating HTML strings, since XHTML expects attributes to have double quotes, and you won't need to escape them.

2 of 5
9

When doing new String(), you are not being returned a string primitive, but a String object.

For all intents and purposes it acts like a string primitive, but there are cases where it won't. For example:

var a = new String('body');
jQuery(a); // This will *not* function as expected
           // This will make a jQuery object containing a "String" object
           // It will NOT treat it as a selector string

Also when comparing things, there may be problem. When you compare objects in JavaScript, it's only true if they are the same exact object, not just the same value. Example:

var a = new String('test');
var b = new String('test');
var c = a;
var d = 'test';

a === b; // false, a and b are different objects (a == b is also false)
c === a; // true, a is the same object as c
c === b; // false, c (which is a) is a different object than b
d === a; // false, d is a primitive and a is an object, they are not equal
'test' === d; // true, they are both string primitives

d == a; // true, this uses "==" so only value is compared, not type

You can use .valueOf() to convert a String object to a string primitive.

new String('test').valueOf() === 'test'; // true

So, I highly suggest using var a = '' or var a = "". As for single quotes vs. double quotes, there is no difference. Consider this example:

var a = "My name is Joe O'Ryan";
var b = 'My name is Joe O\'Ryan'; // need to escape the '
var c = 'Joe said: "Hi"';
var d = "Joe said \"HI\""; // need to escape the "

So, it's up to you whether to use ' or ", but I suggest those over new String().

๐ŸŒ
Pluralsight
pluralsight.com โ€บ tech insights & how-to guides โ€บ software development
How to work with strings in JavaScript | Pluralsight
May 19, 2025 - Here's a basic example of declaring a string using let: let greeting = 'Hi, there!'; console.log(greeting); // Outputs: "Hi, there!" ... "'Hi, there!" is the string value assigned to the variable. It's enclosed in single quotes, which is one way to create a string in JavaScript
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_strings.asp
JavaScript Strings
You can use quotes inside a string, as long as they don't match the quotes surrounding the string: let answer1 = "It's alright"; let answer2 = "He is called 'Johnny'"; let answer3 = 'He is called "Johnny"'; Try it Yourself ยป ยท Templates were introduced with ES6 (JavaScript 2016).
๐ŸŒ
Mozilla
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Guide โ€บ Grammar_and_types
Grammar and types - JavaScript | MDN
This means you don't have to specify the data type of a variable when you declare it. It also means that data types are automatically converted as-needed during script execution. So, for example, you could define a variable as follows: ... Because JavaScript is dynamically typed, this assignment does not cause an error message. In expressions involving numeric and string values with the + operator, JavaScript converts numeric values to strings.
๐ŸŒ
TutorialsTeacher
tutorialsteacher.com โ€บ javascript โ€บ javascript-variable
JavaScript Variables (With Examples)
In JavaScript, a variable can be declared using var, let, const keywords. Learn all about JavaScript variables in detail.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Learn_web_development โ€บ Core โ€บ Scripting โ€บ Variables
Storing the information you need โ€” Variables - Learn web development | MDN
The first time it is called, it should return string, as at that point the myNumber variable contains a string, '500'. Have a look and see what it returns the second time you call it. As well as variables, you can declare constants. These are like variables, except that: ... Similarly, with let you can initialize a variable, and then assign it a new value (this is also called reassigning the variable): ... Note that although a constant in JavaScript must always name the same value, you can change the content of the value that it names.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ what-is-a-string-in-javascript
What is a String in JS? The JavaScript String Variable Explained
November 7, 2024 - In JavaScript, a string is a data type representing a sequence of characters that may consist of letters, numbers, symbols, words, or sentences. We use strings to represent text-based data and we define them using either single quotes ('), double quotes ("), or backticks (``): let name1 = 'John Doe'; let name2 = "John Doe"; let name3 = `John Doe`; In the above example, you have three strings assigned to different variables.