If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

Main content

Storing strings in variables

When we're making a program, we often want to store sequences of letters, like storing “easy” as a game's difficulty level. In programming lingo, we call each letter a character and we call the sequence a string.
Here are examples of storing strings in JavaScript:
var player = "GrayFox";
var storeName = "Baskin Robbins 31 Flavors";
var grade = "A+";
📝 See similar code in: App Lab | Snap | Python
A string can contain any number of characters, including none at all, and those characters can be letters, numbers, or symbols.
Notice how each string is surrounded by double quotes on either side. If you don’t put any quotes around a string, JavaScript will get confused.
🔍 What happens if you forget the quotes? See for yourself!
Uh oh, error!
Here's what happens on that first line: JavaScript looks for a variable named GrayFox. If it could actually find one, it would set the player variable equal to the value of the GrayFox variable. Since it doesn't find one, it errors instead. If you want it to store the literal string, you must surround it in quotes.
✏️ See if you can fix the errors in the program above.
Check your understanding
Which of these are valid lines of JavaScript to store a string?
Choose all answers that apply:

Pseudocode for strings

This pseudocode represents storing a string:
a ← "STRING"
Whenever you see that pseudocode, it means that the variable a (or whatever it's called) is storing the string inside the double quotes.
For example, you might see pseudocode like this:
difficulty ← "medium"
That means the variable difficulty is storing the string "medium".
Let's look at the equivalent code in a few textual languages:
languagecode
JavaScriptvar difficulty = "medium";
Pythondifficulty = "medium"
JavaString difficulty = "medium";
Check your understanding
What is the pseudocode equivalent of this JavaScript?
var hair = "curly";
Choose 1 answer:


🙋🏽🙋🏻‍♀️🙋🏿‍♂️Do you have any questions about this topic? We'd love to answer— just ask in the questions area below!

Want to join the conversation?

  • duskpin sapling style avatar for user Weather
    How do you store quotation marks in strings if they're part of the syntax, and how are line breaks made?
    (9 votes)
    Default Khan Academy avatar avatar for user
  • starky ultimate style avatar for user sebastianbrinezc
    Is possible to get an overflow error in a string variable? I mean like overflow error in number variable.
    (3 votes)
    Default Khan Academy avatar avatar for user
    • aqualine ultimate style avatar for user Martin
      Not like numbers precisely. With numbers you have space and an encoding: For example, say you have 4 bits and you only encode positive numbers in binary:
      Then you can encode all numbers between 0 and 15 (0000 and 1111), if you use that system for something like 8 + 9 you get overflow because 10001 cannot be represented with the for bits. So you might end up with 8 + 9 = 2 (0010).

      Strings are stored as arrays, with each field in the array holding a character.

      With higher-level languages that isn't an issue, with javascript, for example, you just tell your compiler you want a string and you don't have to do anything else on lower languages though you'll have to do that yourself.

      So Khan would be stored in an array like so
      K|a|h|n or rather the binary representation of those letters (K = 75 = 1001011 and so on)
      Now depending on how much space you allocated for the array holding the String you might get overflow, that means you're telling the system to write characters to somewhere that wasn't originally assigned that function.
      (e.g. you have an array with length 10 and you write something like array[20] = 'a').

      That can cause unexpected behaviour and create security vulnerabilities in your code (vulnerability to buffer overflow attacks).

      Unexpected behaviour means something happens, but you don't really know what.

      Hope that helps and it isn't too long.
      (5 votes)
  • male robot hal style avatar for user anonymous
    What is the difference between Javascript and Java?
    (2 votes)
    Default Khan Academy avatar avatar for user
  • stelly blue style avatar for user Misha T.
    Is there a reason you don't have to declare a variable in Python and you have to include the data type of a variable in Java?
    (1 vote)
    Default Khan Academy avatar avatar for user
    • male robot hal style avatar for user Abdelrahman
      Yes, there is a fundamental difference in the way Python and Java handle variables and their data types. Python is a dynamically typed language, while Java is a statically typed language.

      In Python, variables are created dynamically when you assign a value to them, and their type is inferred from the value that is assigned. This means that you don't need to declare the type of a variable explicitly, and you can change its type by assigning a value of a different type to it.

      In Java, on the other hand, you need to declare the type of a variable explicitly when you create it, and that type cannot be changed later. This means that the compiler needs to know the type of every variable at compile time, which is why you need to declare the type of a variable in Java.
      (5 votes)
  • duskpin seed style avatar for user Yunisha
    Hello, I have have just started backusing khan academy,such a great app to learn to code and all the information. My question is if I decide to write this code in pseudocode. Do I still have to use a semicolon after the quotation marks?

    Example: var skittles <= "blue";
    (0 votes)
    Default Khan Academy avatar avatar for user
    • hopper jumping style avatar for user pamela ❤
      From the author:This pseudocode is the pseudocode style that is used by the AP CSP exam and it does not use semi-colons. If you aren't taking the AP CSP exam, I'd suggest learning JavaScript from our Intro to JS course instead- it's more interactive than these articles!
      (5 votes)
  • aqualine ultimate style avatar for user DaDemenGirl_
    If I wanted to create a variable in pseudocode,
    would I do it like this:
    curly <- 10

    or this:
    curly -> 10
    (1 vote)
    Default Khan Academy avatar avatar for user
  • piceratops ultimate style avatar for user SIDTHEKID12345
    How do you change the String to key.code and back again
    (1 vote)
    Default Khan Academy avatar avatar for user
  • aqualine ultimate style avatar for user axel_gomez
    bruh -alan romero
    (1 vote)
    Default Khan Academy avatar avatar for user
  • leaf blue style avatar for user Peregrine [Javar]
    Is there a way to execute code that is in a string? Like:
    "function myFunction () {}" or "if(a){println(b);}"
    (0 votes)
    Default Khan Academy avatar avatar for user
    • starky ultimate style avatar for user KLaudano
      Yes, you can use the eval function to run code stored as a string in JavaScript (e.g. x = eval("3 + 5")). However, YOU SHOULD NOT USE THIS FUNCTION! It is an enormous security risk and regarded as a terrible idea to use. Khan Academy does not even let you use this function in your projects.
      (2 votes)
  • piceratops seed style avatar for user Henrik Van Tassell
    I'm curious why these examples are using var in JS as opposed to let.
    (0 votes)
    Default Khan Academy avatar avatar for user